Collector

Navigation:  LVE-Stats 2 > Creating a Plugin for LVE Stats 2 > Examples of Plugins >

Collector

Previous pageReturn to chapter overviewNext page

Collector's aim is to determine the size of a proper file.

 

# FSize_watcher_collector.py

# Example plugin for monitoring file size.

# Part 1. Collector

 

import os

from lvestats.core.plugin import LveStatsPlugin

 

# Key name

COLLECTOR_KEY = 'FSizeWatcher_fsize'

COLLECTOR_KEY_FILENAME = 'FSizeWatcher_fname'

 

 

class FSize_watcher_collector (LveStatsPlugin):

 # this plugin should be first in chain

 order = 0

 # File to monitoring

 file_to_monitoring = None

 

 def __init__(self):

         pass

 

 # Sets configuration to plugin

 def set_config(self, config):

         self.file_to_monitoring = config.get('file_to_monitoring', None)

         pass

 

 # Work method

 def execute(self, lve_data):

         try:

                 # if monitoring file absent, do nothing

                 if self.file_to_monitoring is None or not os.path.exists(self.file_to_monitoring):

                         return

 

                 # Get file size

                 stat_info = os.stat(self.file_to_monitoring)

                 fsize = stat_info.st_size

 

                 # Place file name and file size to server data dictionary

                 lve_data[COLLECTOR_KEY_FILENAME] = self.file_to_monitoring

                 lve_data[COLLECTOR_KEY] = fsize

         except (OSError, IOError):

                 # file absent or any other error - remove size from dictionary

                 del lve_data[COLLECTOR_KEY]

 

Plugin algorithm is extremely simple – file size is read and written into data dictionary. Files name is read from set_config method configuration. If the name is not specified, then None is written into appropriate variable. All the errors are completely ignored (e.g. if specified file doesn't exist or there's no way to read any of it's information).

 

order attribute is specified as 0 to make this plugin go the first among three. Data collector must always be the first in plugins logical chain, because it provides all the necessary information for the analyzer which goes the next. Specific values of order can be of any kind, but what is important is that when the server starts, all the plugins line up in proper sequence: collector – analyzer – persistor.

 

In order to make plugin work, we have to create configuration file /etc/sysconfig/lvestats.config/FSize_watcher_collector.cfg with the following content:

 

# Config file for FSize_watcher_collector plugin

# Please define monitoring file here

 

#file_to_monitoring = /usr/local/cpanel/logs/error_log

file_to_monitoring = /usr/local/cpanel/logs/access_log

 

Note that file’s name FSize_watcher_collector without .cfg extension matches plugin class name.

 

file_to_monitoring option is read by plugin in set_config method and contains file’s full name for monitoring.

 

Files for monitoring, suggested in the actual example - /usr/local/cpanel/logs/error_log and /usr/local/cpanel/logs/access_log - are real, these are cPanel control panel logs.

 

The first file is errors log; the second is appeal log, is refreshed during common work with panel (e.g. if user email address is changed).

 

Errors log tracking is more important, but appeal log monitoring allows to illustrate plugins work more in details, because it is refreshed more often.

 

Note that plugin can monitor one file only.