Testing log files

One of the many other things Zabbix can do is monitor log files. In this recipe, we will show you how to test your log files with Zabbix for certain patterns.

Getting ready

For this recipe, we need a Zabbix server without agent installed on the server and configured. We also need Zabbix super administrator access.

How to do it ...

Let's say we want to monitor the /var/log/messages file on our OS.

  1. First thing we need to do is make sure Zabbix has access to the file:
    # ll /var/log/messages
    -rw-------. 1 root root 324715 Jan 20 18:54 /var/log/messages
    
  2. As we can see, only the user root has read and write access to this file.
  3. Our next step is to add Zabbix to a new group example, adm; then later we can give this group access to our log file:
    # usermod -a -G adm zabbix
    
  4. Next step is to make the file readable for the group:
    # chmod g+r /var/log/messages
    
  5. Now we only have to add the file messages to the group adm:
    # chgrp adm /var/log/messages
    
  6. Now when we check, our permissions should look like this on the /var/log/messages file:
    # ll /var/log/messages
    -rw-r-----. 1 root adm 327617 Jan 20 19:11 /var/log/messages
    
  7. Our next step is to add an item in our Zabbix server to monitor this file. Go to Configuration | Hosts | and select Item for our Zabbix server. (Or better still, add it to a template that is linked to our Zabbix server).
  8. Click Create item to create a new item.
  9. Give a new Name to our item, example, Errors in /var/log/messages.
  10. Select Zabbix agent (active) as Type.
  11. Add the following Key: log[/var/log/messages,error].
  12. Type of information should be Log.
  13. Update interval can be set to 1.
  14. Now save your item.
    How to do it ...
  15. On the Zabbix server console, type:
    # logger error
    
  16. This will generate an error in our log file, so we can go now to Configuration | Latest data and look how the log file was monitored by our Zabbix server.
  17. Create a Trigger so that we would be alarmed. We go to Configuration | Hosts | Triggers and click on Create trigger.
  18. Give a descriptive name.
  19. Add the following expression : {<template or server>:log[/var/log/messages,error].logsource(error)}=0 so that you get notifications when we get errors in the /var/log/messages file.
    How to do it ...

There's more...

SELinux could be messing with you; so make sure to temporarily disable SELinux to make sure that this is not the problem. In case it is, a rule should be created for this.

The problem with logfile monitoring is that entries in log files do not have a status. If an entry in the log file indicates an error, there is usually no entry indicating that the error has been corrected. So in this case, the trigger will always retain the status error. We have to force Zabbix to update the status and this can be done with the nodata() function. In this case, we have to rewrite our previous trigger like this:

{<template or host>:log[/var/log/messages,error].nodata(300)}=0

In this case, we get an alarm when there is an error in the log file and Zabbix will reset it's status after 120 seconds:

There's more...

In case you want to work with logrotate option, it is very much possible with Zabbix, except that we would have to use logrt option instead of log option.

How it works

Zabbix can look in files for certain keywords; for this, Zabbix needs to have read permissions on those files. In this example, we added Zabbix to the adm group. Then we added our log file to this group and gave the group read permissions. Now by creating the proper item, Zabbix was able to look into the file for out keyword error. With the command logger, we were able to send the command error to our log file and Zabbix picked it up.

Later we saw how it was possible to create the correct trigger for this, and what the possible problem could be with the entry not having a status. To solve this problem, we made use of the nodata function. This function makes it possible for Zabbix to monitor our log file and reset it's status back to normal if no new errors were received for 300 seconds. Of course, in this case you need to be sure that Zabbix is configured to send email, SMS, and so on, else there is a chance that you will not get any notification about the error.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.231