Monitoring Freshclam

Cheers,

im trying to adapt a script on monitoring if the clamav-freshclam service has fetched the current databases from the mirror and iva ran into a problem.

As you probably know the freshclam command usually just works manually after a “killall freshclam”. And therefor i get the output in Icinga

grafik

The service looks like that

def freshclamStatus():
freshclam_stdout = subprocess.Popen([‘freshclam’, ‘status’], stdout=subprocess.PIPE)
freshclam_out = {}
freshclam_out[‘service’] = {}
freshclam_out[‘perfd’] = {}
freshclam_msg = ‘Database failed to update.’
freshclam_status = 2
# Counts if all 3 Databases are reported as up-to-date
freshclam_counter = 0

for line in io.TextIOWrapper(freshclam_stdout.stdout, encoding='UTF-8'):
  if 'up-to-date' in line.split('-> '):
    freshclam_counter = freshclam_counter + 1
if freshclam_counter == 3:
  freshclam_status = 0
if freshclam_status == 0:
  freshclam_msg = 'ClamAV-Freshclam Databases are up-to-date.'
freshclam_out['service']['name']                = 'ClamAV-Freshclam:'
freshclam_out['service']['state']               = freshclam_status
freshclam_out['service']['msg']                 = freshclam_msg
freshclam_out['perfd']['leap_status']           = {}
freshclam_out['perfd']['leap_status']['value']  = freshclam_status
freshclam_out['perfd']['leap_status']['max']    = 2
freshclam_out['perfd']['leap_status']['crit']   = 2
return freshclam_out

Im not that much of a coder, programmer or similar, so please have mercy by your answers which i appreciate a lot anyway.

Thanks

Tobi

If this is python, you need to fix the formatting (indentation) of your code and adding a #! with the path to your python version is also needed.

I think, what you call a service is better called a check command - icinga services call check commands.

Check commands get started by Icinga and like you get the output from freshclam, Icinga gets the output from your script. For this to work, you need to adhere to some conventions for exit codes and stdout formatting.
Have a look at Monitoring Plugins Development Guidelines and monitoring-plugins/CONTRIBUTING.rst at main · Linuxfabrik/monitoring-plugins · GitHub

Oh and don’t over complicate it, it’s fine for a first minimal working prototype, to just return the right of the 4 exit codes (0 = OK, 1 = warn, 2 = crit, 3 = unknown)!

Programming is a iterative process and after a proof of concept a lot of versions are needed to reach the finished product but often a lot of versions in between are still usable if not feature complete. Also most projects never advance from the status of a minimal working prototype :wink:

I think you missunderstood me.

The problem is in the following lines

freshclam_stdout = subprocess.Popen([‘freshclam’, ‘status’], stdout=subprocess.PIPE)
for line in io.TextIOWrapper(freshclam_stdout.stdout, encoding=‘UTF-8’):

because it wanna start a “freshclam” in the command line, which isnt possible since its already up and running and i do not wanna kill the clamav-freshclam.service everytime i execute the script. I kinda need a change of the the subprocess, so that it reads in the current output of “systemctl status clamav-freshclam.service” and not tries to start another freshclam service.

I cut all the stuff about error states and how to report thtat stuff to icinga and the head information, because its not relevant to my problem. U might argue its more sth for a python forum, which is true, but in there ppl have the thing to ask the “why you need this anyway” question which is very annoying.

have a look at monitoring-plugins/check-plugins/systemd-unit at main · Linuxfabrik/monitoring-plugins · GitHub

especially line monitoring-plugins/systemd-unit3 at ee3c0aaea910b651faf7686b012074fe3717acff · Linuxfabrik/monitoring-plugins · GitHub

and lib/shell3.py at e7e6867a2499f3c09dfa8effd512a521e7ef91f5 · Linuxfabrik/lib · GitHub