]> git.ipfire.org Git - oddments/fireinfo.git/commitdiff
Add sendprofile script (version 0.2). v0.2
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Nov 2010 22:33:11 +0000 (23:33 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 24 Nov 2010 22:33:11 +0000 (23:33 +0100)
fireinfo/system.py
sendprofile [new file with mode: 0644]
setup.py

index ccf0910873b40d8149d90d5dcaf97f23f2cb5ced..e3ec004abd018490118bb922415a98a0616f7991 100644 (file)
@@ -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 (file)
index 0000000..0ad0671
--- /dev/null
@@ -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())
index aa586a83e84f7ead85306edb911b14eadf8a74f0..eddb6fdf94d33ff3d8cc90a4ceda9cef4030f0d3 100644 (file)
--- 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"],
 )