#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, getopt
import cookielib, urllib, urllib2
nagios_codes = {'OK': 0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN': 3, 'DEPENDENT': 4}
def usage():
"""
Warning level defaults to 50.0
Critical level defaults to 5.0
"""
nagios_return('UNKNOWN', "usage: {0} [-h|--help] <-u login> <-p password> [-w warning level] [-c critical level]".format(sys.argv[0]))
def nagios_return(code, response):
print code + ": " + response
sys.exit(nagios_codes[code])
def check_condition(login,password,warnval,critval):
login_url = 'https://uslugi.beeline.ru/loginPage.do'
logout_url = 'https://uslugi.beeline.ru/logout.do'
status_url = 'https://uslugi.beeline.ru/vip/prepaid/refreshedPrepaidBalance.jsp'
body = {'userName':login, 'password':password}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
try:
r = opener.open(login_url, urllib.urlencode(body))
except urllib2.HTTPError, e:
return {"code": 'UNKNOWN', "message":"Error code {0}".format(e.code)}
try:
stat = opener.open(status_url)
except urllib2.HTTPError, e:
return {"code": 'UNKNOWN', "message":"Error code {0}".format(e.code)}
statdoc = stat.read()
bal_start = statdoc.find('
')
bal_end = statdoc.find(' ', bal_start)
account_data = statdoc[bal_start+19:bal_end].replace(',','.',1).lstrip()
account_value = float(account_data)
if account_value <= critval:
return {"code": "CRITICAL", "message":account_data}
elif account_value <= warnval:
return {"code": "WARNING", "message":account_data}
elif account_value > warnval:
return {"code": "OK", "message":account_data}
else:
return {"code": "UNKNOWN", "message":account_data}
def main():
if len(sys.argv) < 2:
usage()
try:
opts, args = getopt.getopt(sys.argv[1:], "h:u:p:w:c:", ['--help', '--user', '--password', '--warning', '--critical'])
except getopt.GetoptError:
usage()
argWarning = 50.0
argCritical = 5.0
UserLogin = ''
UserPass = ''
for name, value in opts:
if name in ("-h", "--help"):
usage()
if name in ("-u", "--user"):
if not value.isdigit():
usage()
UserLogin = value
if name in ("-p", "--password"):
if len(value)<3:
usage()
UserPass = value
if name in ("-w", "--warning"):
try:
argWarning = 0.0 + float(value)
except Exception:
print "Unable to convert to floating point value\n"
usage()
if name in ("-c", "--critical"):
try:
argCritical = 0.0 + float(value)
except Exception:
print "Unable to convert to floating point value\n"
usage()
if not UserLogin.isdigit() or len(UserPass)<3:
usage()
result = check_condition(UserLogin, UserPass, argWarning, argCritical)
nagios_return(result['code'], result['message'])
if __name__ == "__main__":
main()
|