]> git.ipfire.org Git - oddments/fireinfo.git/blame - sendprofile
Add more unallowed IDs.
[oddments/fireinfo.git] / sendprofile
CommitLineData
65891720
MT
1#!/usr/bin/python
2
3import json
4import logging
5import logging.handlers
3fde7e27 6import os
65891720
MT
7import sys
8import urllib
9import urllib2
10
11import fireinfo
12
3fde7e27 13ENABLED_FILE = "/var/ipfire/main/send_profile"
a3560310 14PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings"
3fde7e27 15
7c181f39
MT
16log_level = logging.INFO
17if "-d" in sys.argv:
18 log_level = logging.DEBUG
65891720
MT
19
20# Setup logging
21log = logging.getLogger()
22log.setLevel(log_level)
23log.addHandler(logging.handlers.SysLogHandler("/dev/log"))
7c181f39 24log.addHandler(logging.StreamHandler(sys.stderr))
65891720
MT
25for handler in log.handlers:
26 handler.setLevel(log_level)
27
97821efd 28PROFILE_URL = "http://fireinfo.ipfire.org/send/%(public_id)s"
65891720 29
a3560310
MT
30def 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
65891720
MT
46def 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
a3560310
MT
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
65891720 61 try:
65891720
MT
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
76def main():
65891720
MT
77 # Collect system information
78 system = fireinfo.System()
3fde7e27 79 profile = system.profile()
67906550
MT
80
81 # If --dump is passed -> only dump the output.
82 if "--dump" in sys.argv:
3fde7e27 83 print json.dumps(profile, sort_keys=True, indent=4)
67906550
MT
84 return 0
85
d20d3659
MT
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
3fde7e27
MT
94 if not os.path.exists(ENABLED_FILE):
95 del profile["profile"]
96
65891720 97 try:
3fde7e27 98 send_profile(profile)
65891720
MT
99 except urllib2.URLError:
100 return 1
101
102 return 0
103
104sys.exit(main())