]> git.ipfire.org Git - oddments/fireinfo.git/blob - sendprofile
Add more unallowed IDs.
[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 PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings"
15
16 log_level = logging.INFO
17 if "-d" in sys.argv:
18 log_level = logging.DEBUG
19
20 # Setup logging
21 log = logging.getLogger()
22 log.setLevel(log_level)
23 log.addHandler(logging.handlers.SysLogHandler("/dev/log"))
24 log.addHandler(logging.StreamHandler(sys.stderr))
25 for handler in log.handlers:
26 handler.setLevel(log_level)
27
28 PROFILE_URL = "http://fireinfo.ipfire.org/send/%(public_id)s"
29
30 def get_upstream_proxy():
31 if not os.path.exists(PROXY_SETTINGS):
32 return
33
34 proxy_settings = {}
35 with open(PROXY_SETTINGS) as f:
36 for line in f.readlines():
37 k, v = line.split("=", 1)
38 proxy_settings[k] = v.strip()
39
40 return {
41 "host" : proxy_settings.get("UPSTREAM_PROXY", ""),
42 "user" : proxy_settings.get("UPSTREAM_USER", ""),
43 "pass" : proxy_settings.get("UPSTREAM_PASSWORD", ""),
44 }
45
46 def send_profile(profile):
47 logging.debug("Sending profile:")
48 for line in json.dumps(profile, sort_keys=True, indent=4).splitlines():
49 logging.debug(line)
50
51 request = urllib2.Request(PROFILE_URL % profile,
52 data = urllib.urlencode({"profile" : json.dumps(profile)}),
53 )
54
55 # Set upstream proxy if we have one.
56 # XXX this cannot handle authentication
57 proxy = get_upstream_proxy()
58 if proxy["host"]:
59 request.set_proxy(proxy["host"], "http")
60
61 try:
62 urllib2.urlopen(request, timeout=60)
63 except (urllib2.HTTPError, urllib2.URLError), e:
64 reason = "Unknown reason"
65
66 if isinstance(e, urllib2.HTTPError):
67 reason = "%s" % e
68 elif isinstance(e, urllib2.URLError):
69 reason = e.reason
70
71 logging.error("Profile was not sent propertly: %s" % reason)
72 return
73
74 logging.debug("Profile was sent successfully.")
75
76 def main():
77 # Collect system information
78 system = fireinfo.System()
79 profile = system.profile()
80
81 # If --dump is passed -> only dump the output.
82 if "--dump" in sys.argv:
83 print json.dumps(profile, sort_keys=True, indent=4)
84 return 0
85
86 if "--secret-id" in sys.argv:
87 print system.secret_id
88 return 0
89
90 if "--hardware-string" in sys.argv:
91 print system._unique_id
92 return 0
93
94 if not os.path.exists(ENABLED_FILE):
95 del profile["profile"]
96
97 try:
98 send_profile(profile)
99 except urllib2.URLError:
100 return 1
101
102 return 0
103
104 sys.exit(main())