]> git.ipfire.org Git - oddments/fireinfo.git/blob - src/sendprofile
3ce68b9e733c5eeec389cd64299cc6e611320f4c
[oddments/fireinfo.git] / src / sendprofile
1 #!/usr/bin/python
2 ###############################################################################
3 # #
4 # Fireinfo #
5 # Copyright (C) 2010, 2011 IPFire Team (www.ipfire.org) #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 import json
23 import logging
24 import logging.handlers
25 import os
26 import sys
27 import urllib
28 import urllib2
29
30 import fireinfo
31
32 ENABLED_FILE = "/var/ipfire/main/send_profile"
33 PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings"
34
35 log_level = logging.INFO
36 if "-d" in sys.argv:
37 log_level = logging.DEBUG
38
39 # Setup logging
40 log = logging.getLogger()
41 log.setLevel(log_level)
42 log.addHandler(logging.handlers.SysLogHandler("/dev/log"))
43 log.addHandler(logging.StreamHandler(sys.stderr))
44 for handler in log.handlers:
45 handler.setLevel(log_level)
46
47 PROFILE_URL = "https://fireinfo.ipfire.org/send/%(public_id)s"
48
49 def get_upstream_proxy():
50 if not os.path.exists(PROXY_SETTINGS):
51 return {"host" : ""}
52
53 proxy_settings = {}
54 with open(PROXY_SETTINGS) as f:
55 for line in f.readlines():
56 k, v = line.split("=", 1)
57 proxy_settings[k] = v.strip()
58
59 return {
60 "host" : proxy_settings.get("UPSTREAM_PROXY", ""),
61 "user" : proxy_settings.get("UPSTREAM_USER", ""),
62 "pass" : proxy_settings.get("UPSTREAM_PASSWORD", ""),
63 }
64
65 def send_profile(profile):
66 logging.debug("Sending profile:")
67 for line in json.dumps(profile, sort_keys=True, indent=4).splitlines():
68 logging.debug(line)
69
70 request = urllib2.Request(PROFILE_URL % profile,
71 data = urllib.urlencode({"profile" : json.dumps(profile)}),
72 )
73 request.add_header("User-Agent", "fireinfo/%s" % fireinfo.__version__)
74
75 # Set upstream proxy if we have one.
76 proxy = get_upstream_proxy()
77
78 if proxy["host"]:
79 # handling upstream proxies with authentication is more tricky...
80 if proxy["user"] and proxy["pass"]:
81 prx_auth_string = "http://%s:%s@%s/" % (proxy["user"], proxy["pass"], proxy["host"])
82
83 proxy_handler = urllib2.ProxyHandler({'http': prx_auth_string, 'https': prx_auth_string})
84 auth = urllib2.HTTPBasicAuthHandler()
85 opener = urllib2.build_opener(proxy_handler, auth, urllib2.HTTPHandler)
86 urllib2.install_opener(opener)
87 else:
88 request.set_proxy(proxy["host"], "http")
89 request.set_proxy(proxy["host"], "https")
90
91 try:
92 urllib2.urlopen(request, timeout=60)
93 except (urllib2.HTTPError, urllib2.URLError), e:
94 reason = "Unknown reason"
95
96 if isinstance(e, urllib2.HTTPError):
97 reason = "%s" % e
98 elif isinstance(e, urllib2.URLError):
99 reason = e.reason
100
101 logging.error("Profile was not sent propertly: %s" % reason)
102 return
103
104 logging.debug("Profile was sent successfully.")
105
106 def main():
107 # Collect system information
108 system = fireinfo.System()
109 profile = system.profile()
110
111 # If --dump is passed -> only dump the output.
112 if "--dump" in sys.argv:
113 # Remove the private id when dumping the profile because
114 # it contains no information and may confuse people.
115 del profile["private_id"]
116
117 print json.dumps(profile, sort_keys=True, indent=4)
118 return 0
119
120 if "--secret-id" in sys.argv:
121 print system.secret_id
122 return 0
123
124 if "--hardware-string" in sys.argv:
125 print system._unique_id
126 return 0
127
128 if "--public-id" in sys.argv:
129 print system.public_id
130 return 0
131
132 if not os.path.exists(ENABLED_FILE):
133 del profile["profile"]
134
135 try:
136 send_profile(profile)
137 except urllib2.URLError:
138 return 1
139
140 return 0
141
142 sys.exit(main())