zabbix调用python发送短信的操作方法和配置邮件报警类似。过程参考zabbix配置部分。
下面是python的脚本:
[root@zabbix01 alertscripts]# cat sendsms.py
#!/usr/bin/python
import sys,requests,time
MobNum=[sys.argv[1],]
of=open("smslog.log",'a')
of.writelines(MobNum[0])
Subject=sys.argv[2]
of.writelines(Subject)
Content=sys.argv[3]
of.writelines(Content)
print MobNum,Subject,Content
of.close()
ltime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
fout=open("smslog.log","a")
for num in MobNum:
smsr=requests.get("http://xxxwsas.c123.cn/OpenPlatform/OpenApi?action=sendOnce&ac=2342@43563465457&authkey=BB1A5047596FCC2E238EA3EF31F872356A&cgid=8888&csid=6666&m="+num+"&c="+Subject+ltime)
if smsr.status_code==200:
fout.write("message is sended"+ltime+"\r\n")
fout.close()
当调用sendsms.py发送邮件时,老是不成功。查看messages日志发现:
Mar 21 11:42:25 zabbix01 abrt: detected unhandled Python exception in './sendsms.py'
Mar 21 12:02:56 zabbix01 abrt: detected unhandled Python exception in '/etc/zabbix/alertscripts/sendsms.py'
Mar 21 12:02:56 zabbix01 abrtd: Directory 'pyhook-2016-03-21-12:02:56-29245' creation detected
Mar 21 12:02:56 zabbix01 abrt-server[29250]: Saved Python crash dump of pid 29245 to /var/spool/abrt/pyhook-2016-03-21-12:02:56-29245
Mar 21 12:02:56 zabbix01 abrtd: Executable '/etc/zabbix/alertscripts/sendsms.py' doesn't belong to any package and ProcessUnpackaged is set to 'no'
Mar 21 12:02:56 zabbix01 abrtd: 'post-create' on '/var/spool/abrt/pyhook-2016-03-21-12:02:56-29245' exited with 1
网上搜索了一圈,有人说要禁用selinux,有人说要更改/etc/abrt/abrt-action-save-package-data.conf 。我先禁用selinux,然后更改/etc/abrt/abrt-action-save-package-data.conf 配置。改为
ProcessUnpackaged = yes
重启abrtd服务,还是不行。
仔细查看报错,其中提到/var/spool/abrt/pyhook-2016-03-21-12:02:56-29245这个文件,去看看:
[root@zabbix01 ~]# tail /var/spool/abrt/pyhook-2016-03-21-14\:02\:59-5189/
abrt_version count kernel sosreport.tar.xz uuid
analyzer duphash last_occurrence time
architecture event_log machineid type
backtrace executable os_release uid
cmdline hostname reason username
这里我才发现,好东西啊。比如查看reason:
[root@zabbix01 ~]# tail /var/spool/abrt/pyhook-2016-03-21-14\:02\:59-5189/reason
sendsms.py:5:
看到没? 这里提示crash的原因。原来是文件没有权限操作。
再看看cmdline,
[root@zabbix01 ~]# tail /var/spool/abrt/pyhook-2016-03-21-14\:02\:59-5189/cmdline
/usr/bin/python /etc/zabbix/alertscripts/sendsms.py 18911112222 'PROBLEM: /etc/passwd has been changed on 192.100.1.236-TestServer' 'Trigger: /etc/passwd has been changed on 192.100.1.236-TestServer\r\nTrigger status: PROBLEM\r\nTrigger severity: Warning\r\nTrigger URL: \r\n\r\nItem values:\r\n\r\n1. Checksum of /etc/passwd (192.100.1.236-TestServer:vfs.file.cksum[/etc/passwd]): 360585247\r\n2. *UNKNOWN* (*UNKNOWN*:*UNKNOWN*): *UNKNOWN*\r\n3. *UNKNOWN* (*UNKNOWN*:*UNKNOWN*): *UNKNOWN*\r\n\r\nOriginal event ID: 834691'
可以发现,zabbix给我的sendsms.py文件传递了3个参数,正是我想要的。还有很多其他信息,可以逐一查看。对排故来说,太方便了。
通过上面的reason我就大概知道了方向,我把涉及到smslog.log的都改为绝对路径,搞定。代码如下了:
#!/usr/bin/python
#encoding:utf8
import sys,requests,time
#获取第一个变量是手机号码
MobNum=[sys.argv[1],]
#把获取到的内容报错到smslog.log文件中。
of=open("/etc/zabbix/alertscripts/smslog.log",'a')
of.writelines(MobNum[0])
#第二个参数是标题
Subject=sys.argv[2]
of.writelines(Subject)
#第三个参数是短信正文
Content=sys.argv[3]
of.writelines(Content)
#为了方便排故用输出部分数据。
#print MobNum,Subject,Content
of.close()
#获取系统当前时间
ltime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
#把发送成功时间记录到smslog.log文件中。
fout=open("/etc/zabbix/alertscripts/smslog.log","a")
#如果给多个手机号码发送短信,这里循环发送。这里只发送标题将当前时间,正文太长,
for num in MobNum:
smsr=requests.get("http://xxxwsas.c123.cn/OpenPlatform/OpenApi?action=sendOnce&ac=2342@43563465457&authkey=BB1A5047596FCC2E238EA3EF31F872356A&cgid=8888&csid=6666&m="+num+"&c="+Subject+ltime)
#如果返回代码是200,就表示发送成功,并记录到日志文件中。
if smsr.status_code==200:
fout.write("message is sended"+ltime+"\r\n")
#关闭文件写入
fout.close()