OSDN Git Service

------ BUGFIX ------
authorTaizo ITO <taizo@karesansui-project.info>
Tue, 27 Apr 2010 04:00:11 +0000 (13:00 +0900)
committerTaizo ITO <taizo@karesansui-project.info>
Tue, 27 Apr 2010 04:00:11 +0000 (13:00 +0900)
------ BUGZILLA ------

------ TRAC ------

------ IMPROVEMENT ------

------ CREATE ------

------ COMMENT ------
Modified variable name, as the schema of watch table was changed.

karesansui/lib/collectd/action/mail.py
karesansui/lib/collectd/config.py
karesansui/lib/collectd/notification.py
karesansui/lib/collectd/utils.py
karesansui/lib/conf.py
karesansui/lib/parser/collectdplugin.py

index 86ea3f3..fbeb4ac 100644 (file)
@@ -17,7 +17,7 @@ collectdの通知メールを送信する
 引数1:宛先メールアドレス
 引数2:差出人メールアドレス
 引数3:本文
-引数4:件名
+引数4:拡張メッセージ
 引数5:監視項目名
 引数6:ログファイル
 
@@ -34,19 +34,16 @@ from karesansui.lib.net.mail import MAIL_LIB, MAIL_LIB_Exception
 from karesansui.lib.const import KARESANSUI_SYSCONF_DIR
 NOTIF_MAIL_TMPL_DIR = KARESANSUI_SYSCONF_DIR + "/notification"
 
-def send_mail(recipient=None, sender=None, message="", subject="", watch_name="", logfile="/dev/null"):
+def send_mail(recipient=None, sender=None, server="localhost", message="", extra_message="", watch_name="", logfile="/dev/null"):
     retval = False
 
     func_name = sys._getframe(0).f_code.co_name
     append_line(logfile,"[%s] Entering function '%s'." % (func_name,func_name,))
 
-    try:
-        smtp_server
-    except:
-        smtp_server = "localhost"
 
+    smtp_server = server.split(":")[0]
     try:
-        smtp_port
+        smtp_port = int(server.split(":")[1])
     except:
         smtp_port = 25
 
@@ -86,7 +83,9 @@ def send_mail(recipient=None, sender=None, message="", subject="", watch_name=""
         mail_header_file = "%s/%s/mail.header" % (NOTIF_MAIL_TMPL_DIR,lang,)
 
         # Subject
-        if subject is None:
+        try:
+            subject
+        except:
             subject = DEFAULT_SUBJECT
         subject = re.sub("\%\{watch_name\}",watch_name,subject)
         mail.set_subject(subject.encode('utf_8'))
index 1853b20..a5df72b 100644 (file)
@@ -302,18 +302,6 @@ def switch_python_plugin(flag=True, dop=None, webobj=None, host=None):
         dop.cdp_comment("collectdplugin",[PythonName,"LoadPlugin","python"],recursive=True,multiple_file=True)
         dop.cdp_comment("collectdplugin",_keys,recursive=True,multiple_file=True)
 
-    dop.cdp_unset("collectdplugin",["python","Plugin","python","@ORDERS"],multiple_file=True)
-    orders = []
-    orders.append(['Encoding'])
-    orders.append(['LogTraces'])
-    orders.append(['Interactive'])
-    orders.append(['ModulePath'])
-    orders.append(['Import'])
-    orders.append(['Module'])
-
-    dop.cdp_set("collectdplugin",["python","Plugin","python","@ORDERS"],orders,is_opt_multi=True,multiple_file=True)
-
-
 
 def enable_python_plugin(dop=None, webobj=None, host=None):
     global DictOp
index 5875d5b..237a97f 100644 (file)
@@ -366,39 +366,65 @@ def notification(notify=None, data=None):
     check_span         = 60 * 60 * 6
 
     watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds_name)
-    append_log("watch_data     : %s" % (watch_data,) ,4)
+    append_log("watch_data     : %s" % (str(watch_data),) ,4)
+
+    watch_column = [ 'name',
+                     'check_continuation',
+                     'check_span',
+                     'warning_value',
+                     'warning_script',
+                     'warning_mail_body',
+                     'is_warning_percentage',
+                     'is_warning_script',
+                     'is_warning_mail',
+                     'failure_value',
+                     'failure_script',
+                     'failure_mail_body',
+                     'is_failure_percentage',
+                     'is_failure_script',
+                     'is_failure_mail',
+                     'okay_script',
+                     'okay_mail_body',
+                     'is_okay_script',
+                     'is_okay_mail',
+                     'notify_mail_mta',
+                     'notify_mail_from',
+                     'notify_mail_to']
     try:
-        watch_name     = watch_data[0]["name"]
-        watch_email    = watch_data[0]["email"]
-        if severity == NOTIF_OKAY:
-            script_key = "okay_script";
-            mail_key   = "okay_mail";
-        elif severity == NOTIF_WARNING:
-            script_key = "warning_script";
-            mail_key   = "warning_mail";
-        elif severity == NOTIF_FAILURE:
-            script_key = "failure_script";
-            mail_key   = "failure_mail";
-        watch_script = watch_data[0][script_key]
-        watch_mail   = watch_data[0][mail_key]
-        check_span         = watch_data[0]["check_span"]
-        check_continuation = watch_data[0]["continuation"]
+        for column_name in watch_column:
+            exec("%s = watch_data[0]['%s']" % (column_name,column_name,))
+            exec("_var = %s" % (column_name,))
+            append_log("%s: %s"  % (column_name,_var)   ,4)
     except:
         append_log("Error: cannot get watch data." ,1)
         return
         #sys.exit(0)
 
+    if severity == NOTIF_OKAY:
+        watch_script    = okay_script
+        watch_mail_body = okay_mail_body
+        watch_is_script = okay_is_script
+        watch_is_mail   = okay_is_mail
+    elif severity == NOTIF_WARNING:
+        watch_script    = warning_script
+        watch_mail_body = warning_mail_body
+        watch_is_script = warning_is_script
+        watch_is_mail   = warning_is_mail
+    elif severity == NOTIF_FAILURE:
+        watch_script    = failure_script
+        watch_mail_body = failure_mail_body
+        watch_is_script = failure_is_script
+        watch_is_mail   = failure_is_mail
+
+
     # logging
-    append_log("watch_name     : %s" % (watch_name,)   ,4)
-    append_log("watch_email    : %s" % (watch_email,)  ,4)
-    append_log("watch_script   : %s" % (watch_script,) ,4)
-    append_log("watch_mail     : %f" % (watch_mail,)   ,4)
-    append_log("check_span         : %d" % (check_span,)         ,4)
-    append_log("check_continuation : %d" % (check_continuation,) ,4)
+    append_log("watch_script    :%s" % (watch_script,)    ,4)
+    append_log("watch_mail_body :%s" % (watch_mail_body,) ,4)
+    append_log("watch_is_script :%s" % (watch_is_script,) ,4)
+    append_log("watch_is_mail   :%s" % (watch_is_mail,)   ,4)
     append_log("",4)
 
 
-
     ########################################################
     # 前回のヒット時刻を取得(連続しているか判断するため)
     ########################################################
@@ -518,14 +544,15 @@ def notification(notify=None, data=None):
     ########################################################
     # アクション呼出開始
     ########################################################
+
     actions = ACTION_LOG
     try:
-        if watch_script != "":
+        if watch_is_script is True and watch_script != "":
             actions |= ACTION_SCRIPT
     except:
         pass
     try:
-        if watch_mail is True:
+        if watch_is_mail is True and watch_mail_body != "":
             actions |= ACTION_MAIL
     except:
         pass
@@ -578,7 +605,6 @@ def notification(notify=None, data=None):
     if actions & ACTION_SCRIPT:
         from karesansui.lib.collectd.action.script import exec_script
 
-        # !! テストデータ !!
         script = watch_script
         user   = "root"
 
@@ -592,33 +618,32 @@ def notification(notify=None, data=None):
     if actions & ACTION_MAIL:
         from karesansui.lib.collectd.action.mail import send_mail
 
-        # !! テストデータ !!
-        recipient = watch_email
-        recipient = "root@localhost"
-        sender    = None
-        subject   = None
+        smtp_server = notify_mail_mta
+        recipient   = notify_mail_to
+        sender      = notify_mail_from
 
+        extra_msg = ""
         CRLF = "\r\n"
         if actions & ACTION_SCRIPT:
-            alert_msg += CRLF
-            alert_msg += CRLF
+            extra_msg += CRLF
+            extra_msg += CRLF
             if script_retval is False:
-                alert_msg += "Error: failed to execute the following script."
-                alert_msg += CRLF
-                alert_msg += script
-                alert_msg += CRLF
+                extra_msg += "Error: failed to execute the following script."
+                extra_msg += CRLF
+                extra_msg += script
+                extra_msg += CRLF
             else:
-                alert_msg += "Notice: The action script was executed."
-                alert_msg += CRLF
-                alert_msg += "script return value:%s" % script_retval[0]
-                alert_msg += CRLF
+                extra_msg += "Notice: The action script was executed."
+                extra_msg += CRLF
+                extra_msg += "script return value:%s" % script_retval[0]
+                extra_msg += CRLF
                 if len(script_retval[1]) > 0:
-                    alert_msg += "script output:"
-                    alert_msg += CRLF
-                    alert_msg += "%s" % CRLF.join(script_retval[1])
-                    alert_msg += CRLF
+                    extra_msg += "script output:"
+                    extra_msg += CRLF
+                    extra_msg += "%s" % CRLF.join(script_retval[1])
+                    extra_msg += CRLF
 
-        send_mail(recipient=recipient,sender=sender,body=alert_msg,subject=subject,watch_name=watch_name,logfile=logfile)
+        send_mail(recipient=recipient,sender=sender,server=smtp_server,message=watch_mail_body,extra_message=extra_msg,watch_name=watch_name,logfile=logfile)
         pass
 
     """ comment
index 2c9ba5f..fb181d4 100644 (file)
@@ -151,33 +151,55 @@ def query_watch_data(plugin,plugin_instance,type,type_instance,ds):
         for watch in watchs:
             if str(plugin_selector) == str(watch.plugin_selector):
                 name  = watch.name
-                email = watch.created_user.email
-                continuation = watch.continuation_count
-                check_span = watch.prohibition_period
-                warning_value = threshold_value_to_dict(watch.warning_value)
-                warning_percentage = watch.warning_percentage
-                warning_script = watch.warning_script
-                warning_mail = watch.warning_mail
-                failure_value = threshold_value_to_dict(watch.failure_value)
-                failure_percentage = watch.failure_percentage
-                failure_script = watch.failure_script
-                failure_mail = watch.failure_mail
-                okay_script = watch.okay_script
-                okay_mail = watch.okay_mail
-                data = {"name"               : name,
-                        "email"              : email,
-                        "continuation"       : continuation,
-                        "check_span"         : check_span,
-                        "warning_value"      : warning_value,
-                        "warning_percentage" : warning_percentage,
-                        "warning_script"     : warning_script,
-                        "warning_mail"       : warning_mail,
-                        "failure_value"      : failure_value,
-                        "failure_percentage" : failure_percentage,
-                        "failure_script"     : failure_script,
-                        "failure_mail"       : failure_mail,
-                        "okay_script"        : okay_script,
-                        "okay_mail"          : okay_mail,
+
+                check_continuation = watch.continuation_count
+                check_span         = watch.prohibition_period
+
+                warning_value     = threshold_value_to_dict(watch.warning_value)
+                warning_script    = watch.warning_script
+                warning_mail_body = watch.warning_mail_body
+                is_warning_percentage = watch.is_warning_percentage
+                is_warning_script     = watch.is_warning_script
+                is_warning_mail       = watch.is_warning_mail
+
+                failure_value     = threshold_value_to_dict(watch.failure_value)
+                failure_script    = watch.failure_script
+                failure_mail_body = watch.failure_mail_body
+                is_failure_percentage = watch.is_failure_percentage
+                is_failure_script     = watch.is_failure_script
+                is_failure_mail       = watch.is_failure_mail
+
+                okay_script    = watch.okay_script
+                okay_mail_body = watch.okay_mail_body
+                is_okay_script = watch.is_okay_script
+                is_okay_mail   = watch.is_okay_mail
+
+                notify_mail_mta   = watch.notify_mail_mta
+                notify_mail_from  = watch.notify_mail_from
+                notify_mail_to    = watch.notify_mail_to
+
+                data = {"name"                  :name,
+                        "check_continuation"    :check_continuation,
+                        "check_span"            :check_span,
+                        "warning_value"         :warning_value,
+                        "warning_script"        :warning_script,
+                        "warning_mail_body"     :warning_mail_body,
+                        "is_warning_percentage" :is_warning_percentage,
+                        "is_warning_script"     :is_warning_script,
+                        "is_warning_mail"       :is_warning_mail,
+                        "failure_value"         :failure_value,
+                        "failure_script"        :failure_script,
+                        "failure_mail_body"     :failure_mail_body,
+                        "is_failure_percentage" :is_failure_percentage,
+                        "is_failure_script"     :is_failure_script,
+                        "is_failure_mail"       :is_failure_mail,
+                        "okay_script"           :okay_script,
+                        "okay_mail_body"        :okay_mail_body,
+                        "is_okay_script"        :is_okay_script,
+                        "is_okay_mail"          :is_okay_mail,
+                        "notify_mail_mta"       :notify_mail_mta,
+                        "notify_mail_from"      :notify_mail_from,
+                        "notify_mail_to"        :notify_mail_to,
                         }
                 retval.append(data)
     except:
@@ -187,19 +209,45 @@ def query_watch_data(plugin,plugin_instance,type,type_instance,ds):
 
 if __name__ == '__main__':
 
-    plugin          = "df"
+    watch_column = [ 'name',
+                     'check_continuation',
+                     'check_span',
+                     'warning_value',
+                     'warning_script',
+                     'warning_mail_body',
+                     'is_warning_percentage',
+                     'is_warning_script',
+                     'is_warning_mail',
+                     'failure_value',
+                     'failure_script',
+                     'failure_mail_body',
+                     'is_failure_percentage',
+                     'is_failure_script',
+                     'is_failure_mail',
+                     'okay_script',
+                     'okay_mail_body',
+                     'is_okay_script',
+                     'is_okay_mail',
+                     'notify_mail_mta',
+                     'notify_mail_from',
+                     'notify_mail_to']
+
+    plugin          = "memory"
     plugin_instance = ""
-    type            = "df"
-    type_instance   = "boot"
-    ds              = "used"
+    type            = "memory"
+    type_instance   = "used"
+    ds              = "value"
+
+    from karesansui.lib.utils import preprint_r
     watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds)
+    preprint_r(watch_data)
     try:
-        from karesansui.lib.utils import preprint_r
-        preprint_r(watch_data)
-        watch_name     = watch_data[0]["name"]
-        watch_email    = watch_data[0]["email"]
-        watch_warning_value_max = watch_data[0]["warning_value"]["max"]
+        for column_name in watch_column:
+            exec("%s = watch_data[0]['%s']" % (column_name,column_name,))
+            exec("_var = %s" % (column_name,))
+            print "%s: %s"  % (column_name,_var)
     except:
+        print "Error: cannot get watch data."
         sys.exit(0)
 
     pass 
index 46cdff6..6e29108 100644 (file)
@@ -165,8 +165,8 @@ def write_conf(dop, webobj=None, machine=None, modules=[], extra_args={}):
     options = {
          "module"     : ":".join(w_modules),
          "input-file" : ":".join(w_files),
-         "delete"     : None
     }
+    options["delete"] = None
 
     try:
         extra_args['pre-command']
index 91e7fc7..011b5cd 100644 (file)
@@ -86,9 +86,29 @@ class collectdpluginParser:
         #self.dop.preprint_r(self._module)
         return self.dop.getconf(self._module)
 
+    def _pre_write_conf(self,conf_arr={}):
+
+        dop = DictOp()
+        dop.addconf("__",conf_arr)
+
+        if dop.isset("collectdplugin",["python"]) is True:
+            dop.cdp_unset("collectdplugin",["python","Plugin","python","@ORDERS"],multiple_file=True)
+            orders = []
+            orders.append(['Encoding'])
+            orders.append(['LogTraces'])
+            orders.append(['Interactive'])
+            orders.append(['ModulePath'])
+            orders.append(['Import'])
+            orders.append(['Module'])
+            dop.cdp_set("collectdplugin",["python","Plugin","python","@ORDERS"],orders,is_opt_multi=True,multiple_file=True)
+
+        return dop.getconf("__")
+
     def write_conf(self,conf_arr={},extra_args=None,dryrun=False):
         retval = True
 
+        conf_arr = self._pre_write_conf(conf_arr)
+
         for plugin_name,_v in conf_arr.iteritems():
 
             _afile = "%s/%s.conf" % (PARSER_COLLECTD_PLUGIN_DIR,plugin_name,)