]>
Commit | Line | Data |
---|---|---|
65891720 | 1 | #!/usr/bin/python |
3b5ed4e1 MT |
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 | ############################################################################### | |
65891720 MT |
21 | |
22 | import json | |
23 | import logging | |
24 | import logging.handlers | |
3fde7e27 | 25 | import os |
65891720 MT |
26 | import sys |
27 | import urllib | |
28 | import urllib2 | |
29 | ||
30 | import fireinfo | |
31 | ||
3fde7e27 | 32 | ENABLED_FILE = "/var/ipfire/main/send_profile" |
a3560310 | 33 | PROXY_SETTINGS = "/var/ipfire/proxy/advanced/settings" |
3fde7e27 | 34 | |
7c181f39 MT |
35 | log_level = logging.INFO |
36 | if "-d" in sys.argv: | |
37 | log_level = logging.DEBUG | |
65891720 MT |
38 | |
39 | # Setup logging | |
40 | log = logging.getLogger() | |
41 | log.setLevel(log_level) | |
42 | log.addHandler(logging.handlers.SysLogHandler("/dev/log")) | |
7c181f39 | 43 | log.addHandler(logging.StreamHandler(sys.stderr)) |
65891720 MT |
44 | for handler in log.handlers: |
45 | handler.setLevel(log_level) | |
46 | ||
97821efd | 47 | PROFILE_URL = "http://fireinfo.ipfire.org/send/%(public_id)s" |
65891720 | 48 | |
a3560310 MT |
49 | def get_upstream_proxy(): |
50 | if not os.path.exists(PROXY_SETTINGS): | |
3f4e98ab | 51 | return {"host" : ""} |
a3560310 MT |
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 | ||
65891720 MT |
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 | ||
a3560310 MT |
70 | request = urllib2.Request(PROFILE_URL % profile, |
71 | data = urllib.urlencode({"profile" : json.dumps(profile)}), | |
72 | ) | |
d78396e3 | 73 | request.add_header("User-Agent", "fireinfo/%s" % fireinfo.__version__) |
a3560310 MT |
74 | |
75 | # Set upstream proxy if we have one. | |
76 | # XXX this cannot handle authentication | |
77 | proxy = get_upstream_proxy() | |
78 | if proxy["host"]: | |
79 | request.set_proxy(proxy["host"], "http") | |
80 | ||
65891720 | 81 | try: |
65891720 MT |
82 | urllib2.urlopen(request, timeout=60) |
83 | except (urllib2.HTTPError, urllib2.URLError), e: | |
84 | reason = "Unknown reason" | |
85 | ||
86 | if isinstance(e, urllib2.HTTPError): | |
87 | reason = "%s" % e | |
88 | elif isinstance(e, urllib2.URLError): | |
89 | reason = e.reason | |
90 | ||
91 | logging.error("Profile was not sent propertly: %s" % reason) | |
92 | return | |
93 | ||
94 | logging.debug("Profile was sent successfully.") | |
95 | ||
96 | def main(): | |
65891720 MT |
97 | # Collect system information |
98 | system = fireinfo.System() | |
3fde7e27 | 99 | profile = system.profile() |
67906550 MT |
100 | |
101 | # If --dump is passed -> only dump the output. | |
102 | if "--dump" in sys.argv: | |
e8d315a1 MT |
103 | # Remove the private id when dumping the profile because |
104 | # it contains no information and may confuse people. | |
105 | del profile["private_id"] | |
106 | ||
3fde7e27 | 107 | print json.dumps(profile, sort_keys=True, indent=4) |
67906550 MT |
108 | return 0 |
109 | ||
d20d3659 MT |
110 | if "--secret-id" in sys.argv: |
111 | print system.secret_id | |
112 | return 0 | |
113 | ||
114 | if "--hardware-string" in sys.argv: | |
115 | print system._unique_id | |
116 | return 0 | |
117 | ||
bf07eeb1 MT |
118 | if "--public-id" in sys.argv: |
119 | print system.public_id | |
120 | return 0 | |
121 | ||
3fde7e27 MT |
122 | if not os.path.exists(ENABLED_FILE): |
123 | del profile["profile"] | |
124 | ||
65891720 | 125 | try: |
3fde7e27 | 126 | send_profile(profile) |
65891720 MT |
127 | except urllib2.URLError: |
128 | return 1 | |
129 | ||
130 | return 0 | |
131 | ||
132 | sys.exit(main()) |