OSDN Git Service

------ BUGFIX ------
authorTaizo ITO <taizo@karesansui-project.info>
Thu, 6 May 2010 09:56:23 +0000 (18:56 +0900)
committerTaizo ITO <taizo@karesansui-project.info>
Thu, 6 May 2010 09:56:23 +0000 (18:56 +0900)
------ BUGZILLA ------

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

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

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

------ COMMENT ------
Parse body message.

karesansui/lib/collectd/action/mail.py
karesansui/lib/collectd/notification.py
karesansui/lib/collectd/utils.py
karesansui/lib/parser/eml.py

index b4286ee..f94d6cc 100644 (file)
@@ -86,21 +86,32 @@ def send_mail(recipient=None, sender=None, server="localhost", message="", extra
         # カテゴリ用のヘッダとボディを上書きでセット
         rawbody = ""
         try:
+            append_line(logfile,"[%s] message   :%s" % (func_name,message,))
             try:
-                #del headers["Content-Transfer-Encoding"]
-                pass
+                del headers["Content-Transfer-Encoding"]
+                message = message.encode('utf-8')
+                message = str(message)
             except:
                 pass
-            message = str(message)
+            append_line(logfile,"[%s] lang %s" % (func_name,os.environ['LANG']))
             extra_args = {"message":message}
             eml = emlParser().read_conf(extra_args=extra_args)
+            parse_ret = preprint_r(eml,return_var=True)
+            append_line(logfile,"[%s] parse_ret   :%s" % (func_name,parse_ret,))
+
             header  = eml['@message']['value']['header']['value']
             rawbody += eml['@message']['value']['rawbody']['value']
             for _k,_v in header.iteritems():
                 headers[_k] = _v['value']
         except:
             pass
+        try:
+            extra_message = extra_message.encode('utf-8')
+        except:
+            pass
         rawbody += "\n\n" + extra_message
+        append_line(logfile,"[%s] rawbody   :%s" % (func_name,rawbody,))
+        #sys.exit()
 
         mail.set_body(rawbody)
 
@@ -109,6 +120,7 @@ def send_mail(recipient=None, sender=None, server="localhost", message="", extra
 
         #preprint_r(mail.msg._headers)
         for _k,_v in headers.iteritems():
+            append_line(logfile,"[%s] Headers %-12s: %s" % (func_name,_k,_v))
             try:
                 del mail.msg[_k]
             except:
@@ -121,6 +133,12 @@ def send_mail(recipient=None, sender=None, server="localhost", message="", extra
             else:
                 mail.msg[_k] = _v
 
+        try:
+            del mail.msg["Content-Transfer-Encoding"]
+            mail.msg["Content-Transfer-Encoding"] = "base64"
+        except:
+            pass
+
         for _header in mail.msg._headers:
             append_line(logfile,"[%s] Header %-12s: %s" % (func_name,_header[0],_header[1]))
         #sys.exit()
@@ -187,14 +205,16 @@ Failure
 
     """
 
-    message = """From: 
-To: 
-Subject: ほげふが
+    message = """Subject: [Karesansui Notifier] 危険値を越えました。CPU - %{type_instance}
 Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
 
-通知メールのサンプルです。
-CPUプラグイン
-Failure
+レポート時刻: %{current_time}
+
+ホスト %{hostname} の CPU %{type_instance} が危険値を越えました。
+
+危険値は、%{failure_max} に設定されています。
+現在の値は、 %{current_value} です。
 
     """
 
index fe226b0..8f4b1d3 100644 (file)
@@ -282,7 +282,7 @@ def notification(notify=None, data=None):
     ########################################################
     # messageを展開、ds.xxxxをdata_valueとして取り出す
     ########################################################
-    percentage = None       # ThresholdのPercentage用の変数
+    percentage = False
 
     # messageを展開、ds.xxxxをds_xxxとして取り出す
     # Filterの時はdict文字列をexec展開して抽出
@@ -308,13 +308,15 @@ def notification(notify=None, data=None):
         msg = message
         regex  = "^Host (?P<host>.+), plugin (?P<plugin>.+) type (?P<type>.+) \(instance (?P<type_instance>.+)\): "
         regex += "Data source \"(?P<ds_name>.+)\" is currently (?P<ds_value>.+)\. "
-        regex += "That is (below|above) the (failure|warning) threshold of (?P<ts_value>[\-0-9\.]+)\."
+        regex += "That is (below|above) the (failure|warning) threshold of (?P<ts_value>[\-0-9\.]+)(?P<percent_flag>%?)\."
 
         m = re.match(regex,message)
         if m:
-            ds_name    = m.group('ds_name')
-            ds_value   = m.group('ds_value')
-            percentage = m.group('ts_value')
+            ds_name  = m.group('ds_name')
+            ds_value = m.group('ds_value')
+            ts_value = m.group('ts_value')
+            if m.group('percent_flag') == "%":
+                percentage = True
 
     # ds_valueが浮動小数点形式の文字列なら数値に変換
     try:
@@ -340,6 +342,10 @@ def notification(notify=None, data=None):
     except:
         pass
     append_log("ds_value       : %f" % (ds_value,)   ,4)
+    try:
+        append_log("ts_value       : %f" % (ts_value,)   ,4)
+    except:
+        pass
     append_log("percentage     : %s" % (percentage,) ,4)
     append_log("",4)
 
@@ -367,7 +373,7 @@ def notification(notify=None, data=None):
     check_continuation = 60
     check_span         = 60 * 60 * 6
 
-    watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds_name)
+    watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds_name,host=host)
     append_log("watch_data     : %s" % (str(watch_data),) ,4)
 
     watch_column = [ 'name',
@@ -569,27 +575,19 @@ def notification(notify=None, data=None):
 
     # メッセージを作成
     if severity == NOTIF_OKAY:
-        alert_msg = "The value of %s (%f) is out of range." % (name,ds_value,)
+        alert_msg = "The value of %s (%f) is within normal range again." % (name,ds_value,)
     elif severity == NOTIF_WARNING:
         alert_msg = "The value of %s (%f) is within the warning region." % (name,ds_value,)
     elif severity == NOTIF_FAILURE:
         alert_msg = "The value of %s (%f) is within the failure region." % (name,ds_value,)
+
+    # taizo
     try:
-        threshold_min
-        if percentage is None:
-            float(threshold_min)
-            alert_msg += " (min:%f)" % (threshold_min,)
-        else:
-            alert_msg += " (min:%s)" % (threshold_min,)
-    except:
-        pass
-    try:
-        threshold_max
-        if percentage is None:
-            float(threshold_max)
-            alert_msg += " (max:%f)" % (threshold_max,)
+        ts_value
+        if percentage is True:
+            alert_msg += " (threshold:%f%%)" % (float(ts_value),)
         else:
-            alert_msg += " (max:%s)" % (threshold_max,)
+            alert_msg += " (threshold:%f)"   % (float(ts_value),)
     except:
         pass
 
index 8ec50c9..4dbaa3f 100644 (file)
@@ -139,6 +139,10 @@ def query_watch_data(plugin,plugin_instance,type,type_instance,ds,host=None):
     kss_metadata = karesansui.db.get_metadata()
     kss_session  = karesansui.db.get_session()
 
+    myhostname = os.uname()[1]
+    if host == myhostname:
+        host = None
+
     from karesansui.db.access.watch import    \
         findbyall       as w_findbyall,       \
         findby1         as w_findby1,         \
@@ -147,6 +151,7 @@ def query_watch_data(plugin,plugin_instance,type,type_instance,ds,host=None):
         findbyand       as w_findbyand
 
     retval = []
+
     plugin_selector = create_plugin_selector(plugin_instance,type,type_instance,ds,host)
     #import pdb; pdb.set_trace()
     try:
@@ -235,14 +240,16 @@ if __name__ == '__main__':
                      'notify_mail_from',
                      'notify_mail_to']
 
-    plugin          = "memory"
-    plugin_instance = ""
-    type            = "memory"
-    type_instance   = "used"
+    plugin          = "cpu"
+    plugin_instance = "0"
+    type            = "cpu"
+    type_instance   = "user"
     ds              = "value"
+    host            = None
+    host            = "kss7.kss.local.hde.co.jp"
 
     from karesansui.lib.utils import preprint_r
-    watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds)
+    watch_data = query_watch_data(plugin,plugin_instance,type,type_instance,ds,host=host)
     preprint_r(watch_data)
     try:
         for column_name in watch_column:
index f15c34b..fbc5444 100644 (file)
@@ -36,6 +36,8 @@ class emlParser(Parser):
         self.dop.addconf(self._module,{})
 
         self.base_parser_name = Parser.__name__
+
+        self.encoding = "UTF-8"
         pass
 
     def source_file(self):
@@ -90,6 +92,7 @@ class emlParser(Parser):
                 message = open(file).read()
 
             mail = email.Parser.Parser().parsestr(message)
+
             headers  = mail._headers
             msgs     = self._parse_mail(mail)
 
@@ -155,9 +158,15 @@ class emlParser(Parser):
             pass
 
         try:
-            extra_args["message"]
-            if extra_args["message"] != "" and extra_args["message"] is not None:
-                mail = self.parse(message=extra_args["message"])
+            message = extra_args["message"]
+
+            try:
+                message = message.encode(self.encoding)
+            except:
+                pass
+
+            if message != "" and message is not None:
+                mail = self.parse(message=message)
 
                 headers  = mail['headers']
                 msgs     = mail['msgs']