From: Michael Tremer Date: Wed, 24 Nov 2010 22:33:11 +0000 (+0100) Subject: Add sendprofile script (version 0.2). X-Git-Tag: v0.2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=658917203db3d91fd7049fb7b28cb00056cf2a35;p=oddments%2Ffireinfo.git Add sendprofile script (version 0.2). --- diff --git a/fireinfo/system.py b/fireinfo/system.py index ccf0910..e3ec004 100644 --- a/fireinfo/system.py +++ b/fireinfo/system.py @@ -60,7 +60,7 @@ class System(object): "vendor" : self.hypervisor.vendor, } - return json.dumps(p) + return p @property @@ -169,9 +169,4 @@ if __name__ == "__main__": print s.root_disk print s.root_size print "------------\n", s.devices, "\n------------\n" - print s.profile() - - import urllib2 - import urllib - r = urllib2.Request("http://192.168.10.101:9001/send/%s" %s.public_id, data = urllib.urlencode({"profile" : s.profile()})) - urllib2.urlopen(r) \ No newline at end of file + print json.dumps(s.profile(), sort_keys=True, indent=4) diff --git a/sendprofile b/sendprofile new file mode 100644 index 0000000..0ad0671 --- /dev/null +++ b/sendprofile @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import json +import logging +import logging.handlers +import sys +import urllib +import urllib2 + +import fireinfo + +log_level = logging.DEBUG + +# Setup logging +log = logging.getLogger() +log.setLevel(log_level) +log.addHandler(logging.handlers.SysLogHandler("/dev/log")) +log.addHandler(logging.StreamHandler(sys.stdout)) +for handler in log.handlers: + handler.setLevel(log_level) + +PROFILE_URL = "http://stasy.ipfire.org/send/%(public_id)s" + +def send_profile(profile): + logging.debug("Sending profile:") + for line in json.dumps(profile, sort_keys=True, indent=4).splitlines(): + logging.debug(line) + + try: + request = urllib2.Request(PROFILE_URL % profile, + data = urllib.urlencode({"profile" : json.dumps(profile)}), + ) + urllib2.urlopen(request, timeout=60) + except (urllib2.HTTPError, urllib2.URLError), e: + reason = "Unknown reason" + + if isinstance(e, urllib2.HTTPError): + reason = "%s" % e + elif isinstance(e, urllib2.URLError): + reason = e.reason + + logging.error("Profile was not sent propertly: %s" % reason) + return + + logging.debug("Profile was sent successfully.") + +def main(): + logging.info("%s was started." % sys.argv[0]) + + # Collect system information + system = fireinfo.System() + try: + send_profile(system.profile()) + except urllib2.URLError: + return 1 + + return 0 + +sys.exit(main()) diff --git a/setup.py b/setup.py index aa586a8..eddb6fd 100644 --- a/setup.py +++ b/setup.py @@ -3,9 +3,10 @@ from distutils.core import setup, Extension setup( name = "fireinfo", - version = "0.1", + version = "0.2", ext_modules = [ Extension("_fireinfo", ["src/fireinfo.c"]) ], packages = ["fireinfo"], + scripts = ["sendprofile"], )