]> git.ipfire.org Git - oddments/fireinfo.git/blob - sendprofile
Change URL to upload profiles.
[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 logging.info("%s was started." % sys.argv[0])
78
79 # Collect system information
80 system = fireinfo.System()
81 profile = system.profile()
82
83 # If --dump is passed -> only dump the output.
84 if "--dump" in sys.argv:
85 print json.dumps(profile, sort_keys=True, indent=4)
86 return 0
87
88 if not os.path.exists(ENABLED_FILE):
89 del profile["profile"]
90
91 try:
92 send_profile(profile)
93 except urllib2.URLError:
94 return 1
95
96 return 0
97
98 sys.exit(main())