Skip to content

Commit

Permalink
Merge pull request #16 from ARGOeu-Metrics/devel
Browse files Browse the repository at this point in the history
Version 0.3.0
  • Loading branch information
themiszamani authored Apr 4, 2024
2 parents 3a33cdc + 47cabcb commit e97c792
Show file tree
Hide file tree
Showing 15 changed files with 918 additions and 144 deletions.
44 changes: 42 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pipeline {
stages {
stage ('Build'){
parallel {
stage ('Build Centos 7') {
stage ('Build Centos 7 RPM') {
agent {
docker {
image 'argo.registry:5000/epel-7-ams'
Expand All @@ -34,7 +34,7 @@ pipeline {
}
}
}
stage ('Execute tests') {
stage ('Execute tests on CentOS 7') {
agent {
docker {
image 'argo.registry:5000/epel-7-ams'
Expand All @@ -53,6 +53,46 @@ pipeline {
junit '**/junit.xml'
}
}
stage ('Build Rocky 9 RPM') {
agent {
docker {
image 'argo.registry:5000/epel-9-ams'
args '-u jenkins:jenkins'
}
}
steps {
echo 'Building Rpm...'
withCredentials(bindings: [sshUserPrivateKey(credentialsId: 'jenkins-rpm-repo', usernameVariable: 'REPOUSER', \
keyFileVariable: 'REPOKEY')]) {
sh "/home/jenkins/build-rpm.sh -w ${WORKSPACE} -b ${BRANCH_NAME} -d rocky9 -p ${PROJECT_DIR} -s ${REPOKEY}"
}
archiveArtifacts artifacts: '**/*.rpm', fingerprint: true
}
post {
always {
cleanWs()
}
}
}
stage ('Execute tests on Rocky 9') {
agent {
docker {
image 'argo.registry:5000/epel-9-ams'
args '-u jenkins:jenkins -v /dev/log:/dev/log'
}
}
steps {
sh '''
cd $WORKSPACE/$PROJECT_DIR/
rm -f tests/argo_probe_argo_tools
ln -s $PWD/modules/ tests/argo_probe_argo_tools
coverage run -m xmlrunner discover --output-file junit.xml -v tests/
coverage xml
'''
cobertura coberturaReportFile: '**/coverage.xml'
junit '**/junit.xml'
}
}
}
}
}
Expand Down
77 changes: 62 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
# argo-probe-poem-tools

The package contains two probes:
The package contains four probes:

* `check_dirsize.sh` - probe determines the size of a directory (including subdirectories) and compares it with the supplied thresholds.
* `check_file` - probe is checking given file's properties;
* `check_log` - probe is inspecting the application log file for errors;
* `check_file` - probe is inspecting file for content and checking its age.
* `parse_file` - probe is inspecting file for content and checking its age.

## Synopsis

### `check_dirsize.sh` probe

The probe determines the size of a directory (including subdirectories) and compares it with the supplied thresholds (must be defined in KB). It prints the size of the directory in KB followed by "ok" or either "warning" or "critical" if the corresponding threshold is reached.

Example execution of the probe:

```
/usr/libexec/argo/probes/argo_tools/check_dirsize.sh -d /var/spool/ams-publisher -w 10000 -c 100000 -f
OK - /var/spool/ams-publisher size: 732 KB|'size'=732KB;10000;100000
```

### `check_file` probe

The probe checks file properties for the given file. By default, it only checks if the file exists. Optionally, it can check if the file is one of the four types: plain file, directory, socket, or named pipe. Additionally, it is possible to check if file owner or group match the given values. It can also check if file is readable, writable, or executable by user and/or group.

Only mandatory argument is the file path, all the others are optional and except for file types, can be used in any combination.

```
# /usr/libexec/argo/probes/argo_tools/check_file -h
usage: check_file [-h] -F FILE [-f|-d|-s|-p] [-o OWNER] [-g GROUP] [-r] [-w] [-x] [-t TIMEOUT]
ARGO probe that checks file properties; by default, it checks only if it exists
required:
-F FILE, --file FILE file to check
optional:
-f is it a plain file
-d is it a directory
-s is it a socket
-p is it a named pipe
-o OWNER, --owner OWNER
is the file owner matching the one given
-g GROUP, --group GROUP
is the file group matching the one given
-r is it user-or-group readable
-w is it user-or-group writable
-x is it user-or-group executable
-t TIMEOUT, --timeout TIMEOUT
time in seconds after which the probe will stop execution and return CRITICAL (default: 10)
-h, --help Show this help message and exit
```

Example execution of the probe (checking if the file is plain file and writable):

```
# /usr/libexec/argo/probes/argo_tools/check_file -F <PATH-TO-FILE> -f -w -t 20
OK - File OK
```

### `check_log` probe

The probe reports if the application has not been executed in the given time, and raises critical and warning statuses in case there are error or warning messages in the log respectively.
Expand Down Expand Up @@ -37,38 +89,33 @@ Example execution of the probe:
OK - The run finished successfully.
```

### `check_file` probe
### `parse_file` probe

The probe is checking the given file for its modification time - it must be modified less than `<TIME>` hours ago for the probe to return OK. It is also checking if the file contains a `<STRING>` - if it does not, the probe returns CRITICAL.

Mandatory arguments are `<FILE>`, `<TIME>` and `<STRING>`, whereas `<TIMEOUT>` argument has a default value defined.

```
/usr/libexec/argo/probes/argo_tools/check_file -h
usage: check_file [-h] -f FILE -T TIME -s STRING [-t TIMEOUT]
/usr/libexec/argo/probes/argo_tools/parse_file -h
usage: parse_file [-h] -f FILE -T TIME -s STRING [-t TIMEOUT]
Nagios probe for checking file age and content; it raises critical status if
file age is greater than maximum acceptable age or the requested content is
not found in the file
ARGO probe that checks text file age and content; it raises critical status if file age is greater than maximum acceptable age or the requested content is not found in the file
required arguments:
-f FILE, --file FILE Location of file being checked
-T TIME, --time TIME Maximum acceptable time (in hours) since file was last
modified
-T TIME, --time TIME Maximum acceptable time (in hours) since file was last modified
-s STRING, --string STRING
The string which must exist in the file if the probe
is to return OK status
The string which must exist in the file if the probe is to return OK status
optional arguments:
-t TIMEOUT, --timeout TIMEOUT
Time in seconds after which the probe will stop
execution and return CRITICAL (default: 10)
Time in seconds after which the probe will stop execution and return CRITICAL (default: 10)
-h, --help Show this help message and exit
```

Example execution of the probe:

```
/usr/libexec/argo/probes/argo_tools/check_file -f /var/log/ncg.log -T 2 -s "No serious problems were detected during the pre-flight check" -t 30
/usr/libexec/argo/probes/argo_tools/parse_file -f /var/log/ncg.log -T 2 -s "No serious problems were detected during the pre-flight check" -t 30
OK - File is OK
```
8 changes: 6 additions & 2 deletions argo-probe-argo-tools.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Summary: ARGO probe that inspects the application log file for errors
Name: argo-probe-argo-tools
Version: 0.2.0
Version: 0.3.0
Release: 1%{?dist}
Source0: %{name}-%{version}.tar.gz
License: ASL 2.0
Expand All @@ -17,6 +17,7 @@ BuildRequires: python3-devel
%description
ARGO probe that inspects the application log file for errors


%prep
%setup -q

Expand All @@ -36,10 +37,13 @@ rm -rf $RPM_BUILD_ROOT
%files -f INSTALLED_FILES
%defattr(-,root,root)
%dir %{python3_sitelib}/%{underscore %{name}}/
%{python3_sitelib}/%{underscore %{name}}/*.py


%changelog
* Thu Apr 4 2024 Katarina Zailac <kzailac@srce.hr> - 0.3.0-1
- ARGO-4508 Modify check_file probe that it can be used in generic.file.nagios-cmd metric
- ARGO-4507 Include check_dirsize.sh in argo-probe-argo-tools package
- AO-920 Build Rocky 9 RPM for argo-probe-argo-tools package
* Thu Sep 7 2023 Katarina Zailac <katarina.zailac@gmail.com> - 0.2.0-1
- ARGO-4377 Create generic probe that checks file
* Thu Oct 6 2022 Katarina Zailac <katarina.zailac@gmail.com> - 0.1.1-1
Expand Down
61 changes: 57 additions & 4 deletions modules/file.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import datetime
import math
import os
import pathlib
import stat

from argo_probe_argo_tools.utils import does_file_exist, CriticalException
from argo_probe_argo_tools.utils import CriticalException


def now_epoch():
Expand All @@ -12,16 +14,67 @@ def now_epoch():
class File:
def __init__(self, filename):
self.filename = filename
self.path = pathlib.Path(self.filename)
try:
self.mode = os.lstat(self.filename).st_mode

except FileNotFoundError as e:
raise CriticalException(str(e))

def is_file(self):
return os.path.isfile(self.filename)

def is_directory(self):
return stat.S_ISDIR(self.mode)

def is_socket(self):
return stat.S_ISSOCK(self.mode)

def is_fifo(self):
return stat.S_ISFIFO(self.mode)

def check_owner(self, user):
return self.path.owner() == user

def check_group(self, group):
return self.path.group() == group

def is_readable(self):
return bool(self.mode & stat.S_IRUSR) | bool(self.mode & stat.S_IRGRP)

def is_writable(self):
return bool(self.mode & stat.S_IWUSR) | bool(self.mode & stat.S_IWGRP)

def is_executable(self):
return bool(self.mode & stat.S_IXUSR) | bool(self.mode & stat.S_IXGRP)


class TextFile:
def __init__(self, filename):
self.filename = filename

if not self._exists():
raise CriticalException(
f"No such file or directory: '{self.filename}'"
)

if not self._is_file():
raise CriticalException(
f"File '{self.filename}' is not a text file"
)

def _exists(self):
return os.path.exists(self.filename)

def _is_file(self):
return os.path.isfile(self.filename)

def _read(self):
with open(self.filename, "r") as f:
data = [line for line in f.readlines()]

return "\n".join(data)

def check_existence(self):
return does_file_exist(self.filename)

def _get_file_age(self):
return os.path.getmtime(self.filename)

Expand Down
5 changes: 3 additions & 2 deletions modules/log.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import datetime
import os
import time

from argo_probe_argo_tools.utils import does_file_exist, WarnException, \
from argo_probe_argo_tools.utils import WarnException, \
CriticalException


Expand All @@ -17,7 +18,7 @@ def __init__(self, app, logfile, age, timeout):
self.timeout = datetime.timedelta(seconds=timeout)

def check_file_exists(self):
return does_file_exist(self.logfile)
return os.path.exists(self.logfile) and os.path.isfile(self.logfile)

def _read(self):
data = list()
Expand Down
2 changes: 1 addition & 1 deletion modules/nagios.py → modules/status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Nagios:
class Status:
OK = 0
WARNING = 1
CRITICAL = 2
Expand Down
26 changes: 17 additions & 9 deletions modules/utils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import os


def does_file_exist(filename):
if os.path.exists(filename) and os.path.isfile(filename):
return True

else:
return False
import signal


class WarnException(Exception):
Expand All @@ -23,3 +15,19 @@ def __init__(self, msg):

def __str__(self):
return self.msg


class timeout:
def __init__(self, seconds=1, error_message="Timeout"):
self.seconds = seconds
self.error_message = error_message

def handle_timeout(self, signum, frame):
raise TimeoutError(self.error_message)

def __enter__(self):
signal.signal(signal.SIGALRM, self.handle_timeout)
signal.alarm(self.seconds)

def __exit__(self, exc_type, exc_val, exc_tb):
signal.alarm(0)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import glob
from distutils.core import setup

NAME = "argo-probe-argo-tools"
Expand All @@ -23,7 +24,6 @@ def get_ver():
package_dir={'argo_probe_argo_tools': 'modules'},
packages=['argo_probe_argo_tools'],
data_files=[
('/usr/libexec/argo/probes/argo_tools',
['src/check_log', 'src/check_file'])
('/usr/libexec/argo/probes/argo_tools', glob.glob("src/*"))
]
)
Loading

0 comments on commit e97c792

Please sign in to comment.