]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/python | |
2 | ||
3 | import json | |
4 | import logging | |
5 | import logging.handlers | |
6 | import sys | |
7 | import urllib | |
8 | import urllib2 | |
9 | ||
10 | import fireinfo | |
11 | ||
12 | log_level = logging.DEBUG | |
13 | ||
14 | # Setup logging | |
15 | log = logging.getLogger() | |
16 | log.setLevel(log_level) | |
17 | log.addHandler(logging.handlers.SysLogHandler("/dev/log")) | |
18 | log.addHandler(logging.StreamHandler(sys.stdout)) | |
19 | for handler in log.handlers: | |
20 | handler.setLevel(log_level) | |
21 | ||
22 | PROFILE_URL = "http://stasy.ipfire.org/send/%(public_id)s" | |
23 | ||
24 | def send_profile(profile): | |
25 | logging.debug("Sending profile:") | |
26 | for line in json.dumps(profile, sort_keys=True, indent=4).splitlines(): | |
27 | logging.debug(line) | |
28 | ||
29 | try: | |
30 | request = urllib2.Request(PROFILE_URL % profile, | |
31 | data = urllib.urlencode({"profile" : json.dumps(profile)}), | |
32 | ) | |
33 | urllib2.urlopen(request, timeout=60) | |
34 | except (urllib2.HTTPError, urllib2.URLError), e: | |
35 | reason = "Unknown reason" | |
36 | ||
37 | if isinstance(e, urllib2.HTTPError): | |
38 | reason = "%s" % e | |
39 | elif isinstance(e, urllib2.URLError): | |
40 | reason = e.reason | |
41 | ||
42 | logging.error("Profile was not sent propertly: %s" % reason) | |
43 | return | |
44 | ||
45 | logging.debug("Profile was sent successfully.") | |
46 | ||
47 | def main(): | |
48 | logging.info("%s was started." % sys.argv[0]) | |
49 | ||
50 | # Collect system information | |
51 | system = fireinfo.System() | |
52 | try: | |
53 | send_profile(system.profile()) | |
54 | except urllib2.URLError: | |
55 | return 1 | |
56 | ||
57 | return 0 | |
58 | ||
59 | sys.exit(main()) |