Zabbix Call sendsms.py script, unable to send SMS

Source: Internet
Author: User



Zabbix calls Python to send SMS messages in a similar way as configuring mail alerts. The procedure references the Zabbix configuration section.



The following is a Python script:



[email protected] 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&[email protected]& 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 ()



When you call sendsms.py to send a message, it is always unsuccessful. View Messages Log Discovery:



Mar 11:42:25 zabbix01 abrt:detected unhandled Python exception in './sendsms.py '



Mar 12:02:56 zabbix01 abrt:detected unhandled Python exception in '/etc/zabbix/alertscripts/sendsms.py '



Mar 12:02:56 zabbix01 abrtd:directory ' pyhook-2016-03-21-12:02:56-29245 ' creation detected



Mar 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 12:02:56 zabbix01 abrtd:executable '/etc/zabbix/alertscripts/sendsms.py ' doesn ' t belong to any package and Process Unpackaged is set to ' no '



Mar 12:02:56 zabbix01 abrtd: ' post-create ' on '/var/spool/abrt/pyhook-2016-03-21-12:02:56-29245 ' exited with 1



Online Search a circle, some people say to disable selinux, some people say to change /etc/abrt/abrt-action-save-package-data.conf. I disable SELinux first, and then change the /etc/abrt/abrt-action-save-package-data.conf configuration. instead



Processunpackaged = yes



Restart the ABRTD service, or not.



Look closely at the error, which mentions/var/spool/abrt/pyhook-2016-03-21-12:02:56-29245 this file, to see:



[email protected] ~]# 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



Here I found that the good thing ah. For example, view reason:



[email protected] ~]# Tail/var/spool/abrt/pyhook-2016-03-21-14\:02\:59-5189/reason



Sendsms.py:5:<module>:ioerror: [Errno] Permission denied: ' Smslog.log '



Did you see that? Here is a hint of the crash reason. The original file does not have permission to operate.



Look at CmdLine again,



[email protected] ~]# 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 have been changed on 192.100.1.236 -testserver ' Trigger:/etc/passwd have 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 '






Can be found, Zabbix to my sendsms.py file passed 3 parameters, exactly what I want. There are a lot of other information that can be viewed individually. It's too convenient for the platoon.



Through the above reason I will probably know the direction, I have involved smslog.log to the absolute path, done. The code is as follows:



#!/usr/bin/python



#encoding: UTF8



Import Sys,requests,time



The first variable to get is the phone number





MOBNUM = [SYS. ARGV [1],]





Error message to smslog the. Log file.





Of = open ("/etc/zabbix/alertscripts/smslog log ", 'a')





Of writelines (Mobnum [0])





# the second argument is the title





SUBJECT = SYS. ARGV [2]





Of the writelines (Subject)





The third parameter is the text message





The CONTENT = SYS. ARGV [3]





Of writelines (Content)





Output some data to facilitate troubleshooting.





# print mobnum, subject and content











Of the close ()





Gets the current time of the system





Ltime = Time. Strftime (' % % y - m - h: % d % % m: % s', and Time. The localtime (Time. Time ()))





Record the sending time to smslog the. Log file.





Fout = open ("/etc/zabbix/alertscripts/smslog log ", "a")





Where the loop is sent. Here only send the title will the current time, the body is too long,





For num Mobnum in:





Smsr = requests. Get (" Http://xxxwsas.c123.cn/openplatform/openapi? Action = sendonce & [email protected] & Authkey = bb1a5047596fcc2e238ea3ef31f872356a & cgid = 8888 & csid = 6666 & m c = = "+ num +" & "+ Subject + ltime)





If the return code is 200, it indicates that the send was successful and is logged to the log file.





If SMSR. Status_code = = 200:





Fout.write ("message is sended" +ltime+ "\ r \ n")





Close file writing



Fout.close ()






Zabbix calls sendsms.py script and cannot send SMS


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.