]> git.ipfire.org Git - oddments/fireinfo.git/blob - sendprofile
Bump version (0.4).
[oddments/fireinfo.git] / sendprofile
1 #!/usr/bin/python
2
3 import json
4 import logging
5 import logging.handlers
6 import os
7 import sys
8 import urllib
9 import urllib2
10
11 import fireinfo
12
13 ENABLED_FILE = "/var/ipfire/main/send_profile"
14
15 log_level = logging.INFO
16 if "-d" in sys.argv:
17 log_level = logging.DEBUG
18
19 # Setup logging
20 log = logging.getLogger()
21 log.setLevel(log_level)
22 log.addHandler(logging.handlers.SysLogHandler("/dev/log"))
23 log.addHandler(logging.StreamHandler(sys.stderr))
24 for handler in log.handlers:
25 handler.setLevel(log_level)
26
27 PROFILE_URL = "http://stasy.ipfire.org/send/%(public_id)s"
28
29 def send_profile(profile):
30 logging.debug("Sending profile:")
31 for line in json.dumps(profile, sort_keys=True, indent=4).splitlines():
32 logging.debug(line)
33
34 try:
35 request = urllib2.Request(PROFILE_URL % profile,
36 data = urllib.urlencode({"profile" : json.dumps(profile)}),
37 )
38 urllib2.urlopen(request, timeout=60)
39 except (urllib2.HTTPError, urllib2.URLError), e:
40 reason = "Unknown reason"
41
42 if isinstance(e, urllib2.HTTPError):
43 reason = "%s" % e
44 elif isinstance(e, urllib2.URLError):
45 reason = e.reason
46
47 logging.error("Profile was not sent propertly: %s" % reason)
48 return
49
50 logging.debug("Profile was sent successfully.")
51
52 def main():
53 logging.info("%s was started." % sys.argv[0])
54
55 # Collect system information
56 system = fireinfo.System()
57 profile = system.profile()
58
59 # If --dump is passed -> only dump the output.
60 if "--dump" in sys.argv:
61 print json.dumps(profile, sort_keys=True, indent=4)
62 return 0
63
64 if not os.path.exists(ENABLED_FILE):
65 del profile["profile"]
66
67 try:
68 send_profile(profile)
69 except urllib2.URLError:
70 return 1
71
72 return 0
73
74 sys.exit(main())