Notifier

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

Notifier

Previous pageReturn to chapter overviewNext page

Notifier informs system users about any events.

 

# FSize_watcher_saver.py

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

# Part 4. Notifier

 

import time

import smtplib

 

from lvestats.lib.commons import dateutil

from lvestats.core.plugin import LveStatsPlugin

 

# Key name

COLLECTOR_KEY_FSIZE = 'FSizeWatcher_fsize'

COLLECTOR_KEY_FILENAME = 'FSizeWatcher_fname'

 

# email message pattern

EMAIL_MESSAGE_PATTERN = """Hello, administrator!

Size of the file '%s' is %d bytes.

"""

 

 

class FSize_watcher_notifier (LveStatsPlugin):

 # Default period

 DEFAULT_PERIOD_STR = '12h'

 # this plugin should be third in chain

 order = 3

 # Timeout

 timeout = 20

 # Notifier Log filename

 log_file_name = '/var/log/FSize_watcher_notifier.log'

 # Email from address

 email_from = None

 # Email to address

 email_to = None

 # Email subject

 email_subject = None

 

 # Sets configuration to plugin

 def set_config(self, config):

         # Email settings

         self.email_from = config.get('notify_from_email', None)

         self.email_to = config.get('notify_to_email', None)

         self.email_subject = config.get('notify_from_subject', 'Message from FSize_watcher_notifier plugin')

         # Notify period

         s_period = config.get('notify_period', None)

         if s_period:

                 self.period = dateutil.parse_period2(s_period)

         else:

                 self.period = dateutil.parse_period2(FSize_watcher_notifier.DEFAULT_PERIOD_STR)

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

         f.write('%s - FSize_watcher_notifier plugin: configure\n' % time.asctime(time.localtime()))

         f.write('       - Period: %s\n' % self.period)

         f.write('       - From: %s\n' % self.email_from)

         f.write('       - To: %s\n' % self.email_to)

         f.write('       - Subject: \'%s\'\n' % self.email_subject)

         f.close()

 

 # work method

 def execute(self, lve_data):

         if COLLECTOR_KEY_FSIZE not in lve_data or COLLECTOR_KEY_FILENAME not in lve_data:

                 return

         if not self.email_from or not self.email_to:

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

                 f.write('%s - FSize_watcher_notifier plugin error: email_from or email_to not set\n')

                 f.close()

                 return

         try:

                 from email.mime.text import MIMEText

                 # Send email

                 msg = MIMEText(EMAIL_MESSAGE_PATTERN % (lve_data[COLLECTOR_KEY_FILENAME], lve_data[COLLECTOR_KEY_FSIZE]))

                 msg['Subject'] = self.email_subject

                 msg['From'] = self.email_from

                 msg['To'] = self.email_to

 

                 s = smtplib.SMTP('localhost')

                 s.sendmail(self.email_from, [self.email_to], msg.as_string())

                 s.quit()

 

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

                 f.write('%s - FSize_watcher_notifier plugin: email message was successfully sent\n' % time.asctime(time.localtime()))

                 f.close()

         except Exception as e:

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

                 f.write('%s - FSize_watcher_notifier plugin error:\n%s\n' % (time.asctime(time.localtime()), str(e)))

                 f.close()

 

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

 

# Config file for FSize_watcher_notifier.py plugin

# Please define email options here

 

[email protected]

NOTIFY_FROM_SUBJECT=Message from FSize_watcher_notifier

[email protected]

NOTIFY_PERIOD=12h

 

Plugin’s index number equals 3 (order=3), that is why notifier starts after the rest. But since it uses only data formed by collector, then its order may equal any number bigger that collectors order (>0).

 

Notifier reads the necessary parameters from the configuration (email address, topic, period) and writes them into its own log as reference.

 

Plugin’s execute method checks the availability of all the necessary data (email parameters, collectors data) and sends the message. All the notifications are written into the notifier's own log.

 

If any data is missing,  the message is not sent.

 

Log example:

 

Thu Feb  5 11:51:34 2015 - FSize_watcher_notifier plugin: configure

      - Period: 60.0

      - From: [email protected]

      - To: [email protected]

      - Subject: 'Message from FSize_watcher_notifier'

Thu Feb  5 11:51:35 2015 - FSize_watcher_notifier plugin: email message was successfully sent

Thu Feb  5 11:52:35 2015 - FSize_watcher_notifier plugin: email message was successfully sent

Thu Feb  5 11:53:35 2015 - FSize_watcher_notifier plugin: email message was successfully sent

Thu Feb  5 11:54:35 2015 - FSize_watcher_notifier plugin: email message was successfully sent

Thu Feb  5 11:57:00 2015 - FSize_watcher_notifier plugin: configure

      - Period: 43200.0

      - From: [email protected]

      - To: [email protected]

      - Subject: 'Message from FSize_watcher_notifier'

Thu Feb  5 11:57:00 2015 - FSize_watcher_notifier plugin: email message was successfully sent