nagios2jabber : gestion des autres clients que Gajim (ici Pidgin)
authorProgfou <jean-christophe.andre@auf.org>
Mon, 2 Nov 2015 23:02:33 +0000 (18:02 -0500)
committerProgfou <jean-christophe.andre@auf.org>
Mon, 2 Nov 2015 23:02:33 +0000 (18:02 -0500)
nagios/nagios2jabber.py

index cc5f15a..3462e7b 100755 (executable)
 #       command_line    /usr/bin/printf "%b" "Host: $HOSTALIAS$\nState: $HOSTSTATE$\nInfo: $HOSTOUTPUT$\nDate: $LONGDATETIME$" | /usr/local/lib/nagios2jabber.py $CONTACTPAGER$
 #     }
 #     define command {
-#       command_name    notify-service-by-jabber-jc
+#       command_name    notify-service-by-jabber
 #       command_line    /usr/bin/printf "%b" "Service: $SERVICEDESC$\nHost: $HOSTNAME$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\nDate: $LONGDATETIME$" | /usr/local/lib/nagios2jabber.py $CONTACTPAGER$
 #     }
 # - configuration des envois de messages jabber dans /etc/nagios3/conf.d/contacts.cfg
 #     define contact {
-#       contact_name                    jc
 #       ...
 #       host_notification_commands      notify-host-by-jabber,notify-host-by-email
 #       service_notification_commands   notify-service-by-jabber,notify-service-by-email
@@ -63,6 +62,12 @@ if len(sys.argv) != 2 or sys.argv[1] == "-h" or sys.argv[1] == "--help":
 
 dest = sys.argv[1]
 text = sys.stdin.read()
+rich_text = None
+
+# quelques variables dépendant du client
+client_is_pidgin = dest not in ("jean-christophe.andre@auf.org", )
+want_detailed_info = dest in ("moussa.nombre@auf.org", )
+link_fully_supported = not client_is_pidgin
 
 # construit un dictionnaire d'informations
 items = [l.split(":", 1) for l in text.splitlines()]
@@ -112,29 +117,34 @@ if "info" in data:
 #print("data={}".format(data))
 
 # construction des textes
-if "service" in data:
-  text = "{date}\t{state}\t{service} @{host}[{address}]\n{info}".format(**data)
-  enriched_text = "<span style='color: black;'><span>{date}</span> "\
-    "<a href='void();' title='{escaped_info}'>{rich_state}</a> "\
-    "<span style='color: blue;'>{service}</span> "\
-    "@<span style='color: blue;'>{host}</span>"\
-    "[<span style='color: blue;'>{address}</span>]"\
-    "</span>".format(**data)
-    #"<br/><em>{rich_info}</em></span>".format(**data)
-else:
-  text = "{date}\t{state}\t{host}\n{info}".format(**data)
-  enriched_text = "<span style='color: black;'><span>{date}</span> "\
-    "<a href='void();' title='{escaped_info}'>{rich_state}</a> "\
-    "<span style='color: blue;'>{host}</span>"\
-    "</span>".format(**data)
-    #"<br/><em>{rich_info}</em></span>".format(**data)
+try:
+  rich_text_list = [ "<span style='color: black;'><span>{date}</span> " ]
+  if link_fully_supported:
+    rich_text_list.append("<a href='void();' title='{escaped_info}'>{rich_state}</a>")
+  else:
+    rich_text_list.append("{rich_state}")
+  if "service" in data:
+    text = "{date}\t[{state}]\t{service} @{host}[{address}]\n{info}".format(**data)
+    rich_text_list.append(" <span style='color: blue;'>{service}</span> ")
+    rich_text_list.append("@<span style='color: blue;'>{host}</span>")
+    rich_text_list.append("[<span style='color: blue;'>{address}</span>]")
+  else:
+    text = "{date}\t[{state}]\t{host}\n{info}".format(**data)
+    rich_text_list.append(" <span style='color: blue;'>{host}</span>")
+  rich_text_list.append("</span>")
+  rich_text = ("".join(rich_text_list)).format(**data)
+  if want_detailed_info or not link_fully_supported:
+    rich_text = "<p>{}<br/><em>{rich_info}</em></p>".format(rich_text)
+except KeyError:
+  pass
 
 # construction du message
-html = xmpp.Node("html", {"xmlns": "http://jabber.org/protocol/xhtml-im"})
-html.addChild(node=xmpp.simplexml.XML2Node(
-  "<body xmlns='http://www.w3.org/1999/xhtml'>{}</body>".format(enriched_text)))
 message = xmpp.protocol.Message(to=dest, body=text, typ="chat")
-message.addChild(node=html)
+if rich_text is not None:
+  html = xmpp.Node("html", {"xmlns": "http://jabber.org/protocol/xhtml-im"})
+  html.addChild(node=xmpp.simplexml.XML2Node(
+    "<body xmlns='http://www.w3.org/1999/xhtml'>{}</body>".format(rich_text)))
+  message.addChild(node=html)
 
 # envoi du message
 jid = xmpp.protocol.JID(_CONFIG["jid"])