Persistor

Navigation:  Limits > Command Line Tools > lve-stats 2 > Creating a Plugin for LVE Stats 2 > Examples of Plugins >

Persistor

Previous pageReturn to chapter overviewNext page

Persistor saves information from the common dictionary into files, databases, etc.

 

# FSize_watcher_saver.py

# Example plugin for monitoring file size and last modification date-time.

# Part 3. Data saver

 

import signal

import sys

import time

from lvestats.core.plugin import LveStatsPlugin

 

# Key name 1 for saver plugin

SAVER_KEY = 'FSizeWatcher_fsize_to_store'

# Key name 2 for saver plugin

SAVER_DATA_PRESENCE = 'FSizeWatcher_fsize_present'

# Monitoring file name

COLLECTOR_KEY_FILENAME = 'FSizeWatcher_fname'

 

 

class FSize_watcher_saver (LveStatsPlugin):

 # this plugin should be third in chain

 order = 2

 # Plugin run period in seconds

 period = 60

 # Log filename

 log_file_name = None

 # First run flag

 is_first_run = True

 

 def __init__(self):

         signal.signal(signal.SIGTERM, self.sigterm_handler)

 

 # Sets configuration to plugin

 def set_config(self, config):

         # Get log filename

         self.log_file_name = config.get('log_filename', None)

 

 # work method

 def execute(self, lve_data):

         # do nothing, if log file not defined

         if not self.log_file_name:

                 return

         try:

                 # Check presence data

                 if SAVER_DATA_PRESENCE not in lve_data or lve_data[SAVER_DATA_PRESENCE] == 0:

                         # No data

                         return

                 # Get file size from server data dictionary

                 fsize = lve_data[SAVER_KEY]

 

                 # Store data to log

                 f = open(self.log_file_name, 'a')

                 if self.is_first_run:

                         f.write('%s - FSize_watcher started. Monitoring file: %s, saving data period=%d sec\n' % (time.asctime(time.localtime()), lve_data[COLLECTOR_KEY_FILENAME], self.period))

                         self.is_first_run = False

                 f.write('%s - FSize_watcher: file size is %d bytes\n' % (time.asctime(time.localtime()), fsize))

                 f.close()

         except:

                 # Ignore all errors

                 pass

 

 # Terminate handler

 def sigterm_handler(self, signum, frame):

         if self.log_file_name:

                 try:

                         # Store data to log file

                         f = open(self.log_file_name, 'a')

                         f.write('%s - File watcher saver plugin: TERMINATE\n' % time.asctime(time.localtime()))

                         f.close()

                         pass

                 except:

                         # Ignore all errors

                         pass

         # Terminate process

         sys.exit(0)

 

Configuration file /etc/sysconfig/lvestats.config/FSize_watcher_saver.cfg:

 

# Config file for FSize_watcher_saver.py plugin

# Please define log filename here

 

log_filename = /var/log/FSize_watcher.log

 

This plugin starts after analyzer (order=2), checks new file size presence flag, and if positive – writes it into log. If the flag is cleared (which means the size hasn't changed), then plugin simply ends.

 

Starts once in a minute (period=60).

 

Also this plugin shows the work of signal handler.

 

Plugin constructor registers handler-function of a  proper signal: signal.signal(signal.SIGTERM, self.sigterm_handler). This means, that when the server finishes its work, then sigterm_handler method of plugin class will be invoked. In the actual example the function just writes a notification into log, tracing the fact of it's invocation.

 

Pay attention on sys.exit(0) command in the end of the handler. Find the information on it in Server Plugin Arrangement section.

 

In addition see into examples of file log /var/log/FSize_watcher.log formed by the plugins above:

 

Tue Feb  3 13:06:24 2015 - FSize_watcher started. Monitoring file: /usr/local/cpanel/logs/access_log, saving data period=60 sec

Tue Feb  3 13:06:24 2015 - FSize_watcher: file size is 122972890 bytes

Tue Feb  3 13:07:25 2015 - FSize_watcher: file size is 122975507 bytes

Tue Feb  3 13:08:25 2015 - FSize_watcher: file size is 122978124 bytes

Tue Feb  3 13:09:25 2015 - FSize_watcher: file size is 122978997 bytes

Tue Feb  3 13:10:25 2015 - FSize_watcher: file size is 122981033 bytes

Tue Feb  3 13:11:25 2015 - FSize_watcher: file size is 122982052 bytes

Tue Feb  3 13:13:25 2015 - FSize_watcher: file size is 122983798 bytes

Tue Feb  3 13:20:15 2015 - File watcher saver plugin: TERMINATE

 

and

 

Thu Feb  5 13:07:27 2015 - FSize_watcher started. Monitoring file: /usr/local/cpanel/logs/error_log, saving data period=60 sec

Thu Feb  5 13:07:27 2015 - FSize_watcher: file size is 14771849 bytes

Thu Feb  5 14:03:32 2015 - FSize_watcher: file size is 14771995 bytes

Thu Feb  5 15:01:36 2015 - FSize_watcher: file size is 14772434 bytes

Thu Feb  5 17:15:47 2015 - FSize_watcher: file size is 14772873 bytes

Thu Feb  5 18:47:54 2015 - FSize_watcher: file size is 14775213 bytes

Thu Feb  5 19:11:56 2015 - FSize_watcher: file size is 14775652 bytes

Thu Feb  5 21:09:05 2015 - FSize_watcher: file size is 14776091 bytes

Thu Feb  5 23:06:14 2015 - FSize_watcher: file size is 14776530 bytes

Fri Feb  6 00:47:23 2015 - FSize_watcher: file size is 14778870 bytes

Fri Feb  6 01:02:24 2015 - FSize_watcher: file size is 14779309 bytes

Fri Feb  6 02:00:28 2015 - FSize_watcher: file size is 14779434 bytes

Fri Feb  6 03:16:34 2015 - FSize_watcher: file size is 14779873 bytes

Fri Feb  6 05:04:42 2015 - FSize_watcher: file size is 14779998 bytes

Fri Feb  6 05:12:43 2015 - FSize_watcher: file size is 14780437 bytes

Fri Feb  6 05:56:50 2015 - FSize_watcher: file size is 14780551 bytes

Fri Feb  6 06:01:50 2015 - FSize_watcher: file size is 14780975 bytes

Fri Feb  6 06:03:51 2015 - FSize_watcher: file size is 14782183 bytes

Fri Feb  6 06:04:51 2015 - FSize_watcher: file size is 14782575 bytes

Fri Feb  6 06:18:52 2015 - FSize_watcher: file size is 14782647 bytes

Fri Feb  6 06:21:52 2015 - FSize_watcher: file size is 14782898 bytes

Fri Feb  6 06:48:54 2015 - FSize_watcher: file size is 14785238 bytes

Fri Feb  6 07:09:56 2015 - FSize_watcher: file size is 14785677 bytes

Tue Feb  6 08:03:15 2015 - File watcher saver plugin: TERMINATE

 

You can see that log record is being held once a minute (what we actually need), new file size is written.

 

Also we can notice that handler SIG_TERM was executed, signaling that plugin received the notification about server shut-down.