Pkgname_ = "* * *"
Version_ = "1.0"
Import Sys
Import OS
Basepath_ = Os.path.dirname (Os.path.realpath (__file__))
Modulepath_ = Os.path.join (basepath_, "modules")
Custompath_ = Os.path.join (basepath_, "Custom")
Configpath_ = Os.path.join (basepath_, "config")
Sys.path.append (Os.path.join (modulepath_, "config"))
Sys.path.append (Os.path.join (modulepath_, "parser"))
Sys.path.append (Os.path.join (Modulepath_, "requester"))
Sys.path.append (Os.path.join (Modulepath_, "transporter"))
Sys.path.append (Os.path.join (modulepath_, "decision"))
Import Optparse
Import datetime
Import Imp
Import time
Import Parser_
Import requester
Import decision
Import Transporter
def routine_ (source):
#start parse!
Parser = Parser_. Parser (source)
URLs = Parser.execute ()
if (Parser.errorcode ()):
Code = Parser.errorcode ()
Print "Error occured while parssing. Code:%s; Msg:%s "% \
(Code, PARSER.ERRORMSG ())
Sys.exit (1)
#start Request
Req = requester. Requester (URLs)
Rresult = Req.execute ()
if (Req.errorcode ()):
Print "Error occured while request. Code:%s; Msg:%s "% \
(Req.errorcode (), req.errormsg ())
Sys.exit (1)
#start decision
DEC = decision. Decision (Rresult)
Dresult = Dec.execute ()
if (Dec.errorcode ()):
Print "Error eccoured while request. Code:%s; Msg:%s "% \
Dec.errorcode (), dec.errormsg ()
#result transport to log, show or MySQL
trans = transporter. Transporter (Result=dresult, mysql=saver["MySQL"], \
Erronly=erronly, log=saver["Log"], stdout=saver["stdout"], \
Alert=opt.alert, zabbix=saver["Zabbix"])
TResult = Trans.execute ()
if (Trans.errorcode ()):
Print "Error occured while transport result. Code:%s; " +\
"msg:%s"% Trans.errorcode (), trans.errormsg ()
Sys.exit (1);
if __name__ = = "__main__":
Start = Datetime.datetime.now ()
#Option Parser
Optparser = Optparse. Optionparser (usage= "%s"% __doc__.strip (), \
version=pkgname_+ ' +version_)
Optparser.add_option ("-M", "--mysql", dest= "MySQL", help= "specify MySQL source" \
+ "of the data config,specify a MySQL config file")
Optparser.add_option ("-D", "--dmysql", dest= "Dmysql", help= "specify MySQL source" \
+ "of the dynamic IPs, specify a MySQL config file")
Optparser.add_option ("-F", "--file", dest= "file", help= "Specify a local INI file" \
+ "As the source Config data")
Optparser.add_option ("-P", "--param", dest= "param", help= "write the config source" \
+ "data directly")
Optparser.add_option ("-Y", "--resultmysql", dest= "Rmysql", help= "set a SQL" \
+ "to store the Results,set 1 to use the source MySQL config.if only want" \
+ "To save error result add ': Error ' at the end" \
+ "e.g.-y/home/louis/sql.ini:error")
Optparser.add_option ("-L", "--resultfile", dest= "Rlog", help= "set a file" \
+ "To save the Results,error message only Plus:error" \
+ "e.g.-l/home/louis/log.txt:error")
Optparser.add_option ("-S", "--resultstdout", dest= "Rstdout", help= "if set" \
+ "As Stdout,result would be printed plus:error if only want to print" \
"Error result,e.g.-S Stdout:error")
Optparser.add_option ("-A", "--alert", dest= "alert", action= "Store_true", help= "if set" \
+ ", Urlwatch would send alert message if error occured")
Optparser.add_option ("-Z", "--zabbix", dest= "Zabbix", help= "location of the L2zabbix" \
+ "source file")
Optparser.add_option ("-E", "--deamon", dest= "daemon", help= "do routine in a unlimited" \
+ "loop")
Optparser.set_defaults (Mysql=false, Inifile=false, Param=false, Rstdout=false,
Rmysql=false, Rlog=false)
(opt, args) = Optparser.parse_args (SYS.ARGV)
If not (Opt.file or opt.mysql or Opt.param or opt.dmysql):
Print "At least one source should be specified"
Sys.exit (1)
If not (opt.rstdout or opt.rmysql or Opt.rlog or Opt.zabbix):
Print "At least one result saver should is specified"
Sys.exit (1)
Source = {
"Local": Opt.file,
"MySQL": Opt.mysql,
"param": Opt.param,
"Dmysql": Opt.dmysql,
}
Rmysql = Opt.rmysql.split (":") if opt.rmysql else False
Rlog = Opt.rlog.split (":") if Opt.rlog else False
Rstdout = Opt.rstdout.split (":") if opt.rstdout else False
Saver = {
"MySQL": rmysql[0] If rmysql else False,
"Log": rlog[0] If rlog else False,
"stdout": rstdout[0] If rstdout else False,
"Zabbix": Opt.zabbix
}
Erronly = {
"MySQL": True if (Rmysql and Len (rmysql) = = 2 and rmysql[1] = = "error") \
Else False,
"Log": True if (Rlog and Len (rlog) = = 2 and rlog[1] = = "error") \
Else False,
"stdout": True if (Rstdout and Len (rstdout) = = 2 and rstdout[1] = = "error") \
Else False,
}
If saver["MySQL"] = = "1" and source["MySQL"]:
saver["MySQL"] = source["MySQL"]
If Opt.daemon:
While True:
Routine_ (source)
Time.sleep (float (Opt.daemon))
Else
Routine_ (source)
This article from "Expect volume synchronization data" blog, declined reprint!
Personal Record 1