Last active
August 29, 2015 14:02
-
-
Save swdream/ce5084aba0a88c3c19cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@cloud-monitor-1:/etc/shinken# cat /usr/lib/nagios/plugins/notify_by_xmpp.py | |
#!/usr/bin/python -tt | |
# [email protected], modified by David Laval | |
# gplv2+ | |
## XMPP notification | |
#define command{ | |
# command_name notify-host-by-xmpp | |
# command_line $PLUGINSDIR$/notify_by_xmpp.py -a $PLUGINSDIR$/notify_by_xmpp.ini "Host '$HOSTALIAS$' is $HOSTSTATE$ - Info : $HOSTOUTPUT$" $CONTACTEMAIL$ | |
#} | |
# | |
#define command{ | |
# command_name notify-service-by-xmpp | |
# command_line $PLUGINSDIR$/notify_by_xmpp.py -a $PLUGINSDIR$/notify_by_xmpp.ini "$NOTIFICATIONTYPE$ $HOSTNAME$ $SERVICED ESC$ $SERVICESTATE$ $SERVICEOUTPUT$ $LONGDATETIME$" $CONTACTEMAIL$ | |
#} | |
# needs a config file to get username/pass/other info format is: | |
#[xmpp_account] | |
#server=jabber.org | |
#port=5222 | |
#username=yourusername | |
#password=yourpasssword | |
#resource=monitoring | |
defaults = {'server':'xmpp.vccloud.vn', | |
'port':'5222', | |
'resource':'monitoring'} | |
# until xmppony is inplace | |
import warnings | |
warnings.simplefilter("ignore") | |
import xmpp | |
from xmpp.protocol import Message | |
from optparse import OptionParser | |
import ConfigParser | |
import sys | |
import os | |
import logging | |
logging.basicConfig(filename="/var/log/shinken/xmpp.log", level=logging.DEBUG) | |
logger = logging.getLogger(__name__) | |
parser = OptionParser() | |
parser.add_option("-a", dest="authfile", default=None, help="file to retrieve username/password/server/port/resource information from") | |
opts, args = parser.parse_args() | |
conf = ConfigParser.ConfigParser(defaults=defaults) | |
if not opts.authfile or not os.path.exists(opts.authfile): | |
logger.error("no config/auth file specified, can't continue") | |
print "no config/auth file specified, can't continue" | |
sys.exit(1) | |
conf.read(opts.authfile) | |
if not conf.has_section('xmpp_account') or not conf.has_option('xmpp_account', 'username') or not conf.has_option('xmpp_account', 'password'): | |
logger.error("cannot find at least one of: config section 'xmpp_account' or username or password") | |
print "cannot find at least one of: config section 'xmpp_account' or username or password" | |
sys.exit(1) | |
server = conf.get('xmpp_account', 'server') | |
username = conf.get('xmpp_account', 'username') | |
password = conf.get('xmpp_account', 'password') | |
resource = conf.get('xmpp_account', 'resource') | |
port = conf.get('xmpp_account', 'port') | |
if len(args) < 1: | |
logger.error("xmppsend message [to whom, multiple args]") | |
print "xmppsend message [to whom, multiple args]" | |
sys.exit(1) | |
msg = args[0] | |
msg = msg.replace('\\n', '\n') | |
c = xmpp.Client(server=server, port=port, debug=[]) | |
con = c.connect() | |
if not con: | |
logger.error("Error: could not connect to server: %s:%s" % (c.Server, c.Port)) | |
print "Error: could not connect to server: %s:%s" % (c.Server, c.Port) | |
sys.exit(1) | |
auth = c.auth(user=username, password=password, resource=resource) | |
if not auth: | |
logger.error("Error: Could not authenticate to server: %s:%s" % (c.Server, c.Port)) | |
print "Error: Could not authenticate to server: %s:%s" % (c.Server, c.Port) | |
sys.exit(1) | |
if len(args) < 2: | |
r = c.getRoster() | |
for user in r.keys(): | |
if user == username: | |
continue | |
c.send(Message(user, '%s' % msg)) | |
logger.info(("send xmpp message to %s" %user)) | |
else: | |
for user in args[1:]: | |
c.send(Message(user, '%s' % msg)) | |
logger.info(("send xmpp message to %s" %user)) | |
root@cloud-monitor-1:/etc/shinken# cat /usr/lib/nagios/plugins/notify_by_xmpp.ini | |
[xmpp_account] | |
server=xmpp.vccloud.vn | |
port=5222 | |
[email protected] | |
password=xxxxx | |
resource=monitoring | |
Chay lenh: | |
root@cloud-monitor-1:/etc/shinken# /usr/lib/nagios/plugins/notify_by_xmpp.py -a /usr/lib/nagios/plugins/notify_by_xmpp.ini "hehe" [email protected] | |
An error occurred while looking up _xmpp-client._tcp.xmpp.vccloud.vn | |
Traceback (most recent call last): | |
File "/usr/lib/nagios/plugins/notify_by_xmpp.py", line 81, in <module> | |
con = c.connect() | |
File "/usr/lib/python2.7/dist-packages/xmpp/client.py", line 200, in connect | |
if not CommonClient.connect(self,server,proxy,secure,use_srv) or secure<>None and not secure: return self.connected | |
File "/usr/lib/python2.7/dist-packages/xmpp/client.py", line 184, in connect | |
if not self.Process(1): return | |
File "/usr/lib/python2.7/dist-packages/xmpp/dispatcher.py", line 303, in dispatch | |
handler['func'](session,stanza) | |
File "/usr/lib/python2.7/dist-packages/xmpp/dispatcher.py", line 215, in streamErrorHandler | |
raise exc((name,text)) | |
xmpp.protocol.HostUnknown: (u'host-unknown', '') | |
=> looi dang nay | |
con = c.connect() | |
https://gist.github.com/swedream/ce5084aba0a88c3c19cf#file-gistfile1-txt-L82 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment