]>
git.ipfire.org Git - oddments/fireinfo.git/blob - sendprofile
5 import logging
.handlers
13 ENABLED_FILE
= "/var/ipfire/main/send_profile"
14 PROXY_SETTINGS
= "/var/ipfire/proxy/advanced/settings"
16 log_level
= logging
.INFO
18 log_level
= logging
.DEBUG
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
)
28 PROFILE_URL
= "http://fireinfo.ipfire.org/send/%(public_id)s"
30 def get_upstream_proxy():
31 if not os
.path
.exists(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()
41 "host" : proxy_settings
.get("UPSTREAM_PROXY", ""),
42 "user" : proxy_settings
.get("UPSTREAM_USER", ""),
43 "pass" : proxy_settings
.get("UPSTREAM_PASSWORD", ""),
46 def send_profile(profile
):
47 logging
.debug("Sending profile:")
48 for line
in json
.dumps(profile
, sort_keys
=True, indent
=4).splitlines():
51 request
= urllib2
.Request(PROFILE_URL
% profile
,
52 data
= urllib
.urlencode({"profile" : json
.dumps(profile
)}),
55 # Set upstream proxy if we have one.
56 # XXX this cannot handle authentication
57 proxy
= get_upstream_proxy()
59 request
.set_proxy(proxy
["host"], "http")
62 urllib2
.urlopen(request
, timeout
=60)
63 except (urllib2
.HTTPError
, urllib2
.URLError
), e
:
64 reason
= "Unknown reason"
66 if isinstance(e
, urllib2
.HTTPError
):
68 elif isinstance(e
, urllib2
.URLError
):
71 logging
.error("Profile was not sent propertly: %s" % reason
)
74 logging
.debug("Profile was sent successfully.")
77 logging
.info("%s was started." % sys
.argv
[0])
79 # Collect system information
80 system
= fireinfo
.System()
81 profile
= system
.profile()
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)
88 if not os
.path
.exists(ENABLED_FILE
):
89 del profile
["profile"]
93 except urllib2
.URLError
: