Last active
August 29, 2015 13:56
-
-
Save liling/9269929 to your computer and use it in GitHub Desktop.
检查 gnokii smsd 短信猫所对应的中国移动神州行电话卡的余额,需配合 python-pynag 和 python-mysqldb 使用
This file contains hidden or 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
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| # 检查 gnokii smsd 连接的短信猫对应的电话卡的余额,并返回给 nagios | |
| # 使用方法为 check_smsd_balance -c 20: -w 100: -p 2 | |
| import MySQLdb, re, string | |
| ## This is for the custom nagios module | |
| from pynag.Plugins import simple as Plugin | |
| ## Create the plugin option | |
| np = Plugin() | |
| ## Add a command line argument | |
| np.add_arg("p","phone", "Enter the phone id", required=True) | |
| ## This starts the actual plugin activation | |
| np.activate() | |
| balance = -1 | |
| db = MySQLdb.connect(host="localhost", port=3306, user="root", passwd="", db="smsd", use_unicode=True, charset="utf8") | |
| cursor = db.cursor() | |
| rows = cursor.execute(u"SELECT text FROM inbox WHERE phone=%d AND number='10086' AND text LIKE '查询余额服务%%' AND TIMEDIFF(now(), insertdate) < '30:00:00' ORDER BY insertdate DESC LIMIT 1" % string.atoi(np['phone'])) | |
| if rows > 0: | |
| data = cursor.fetchone() | |
| if data: | |
| p = re.compile(u"^查询余额服务:您总账户余额为([\.0-9]+)元。") | |
| m = p.findall(data[0]) | |
| if m: | |
| balance = string.atof(m[0]) | |
| ## Add the perdata | |
| np.add_perfdata("balance", balance) | |
| ## Do the check | |
| np.check_range(balance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你好,问下gnokii smsd 这个程序你运行正常吗?为什么我启动这个程序后,就没办法通过gnokii 接着进行发送短信了,而且存到mysql 数据库中的中文都是乱码, 我已经设置为UTF8 字符集了。