-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathutils.py
More file actions
44 lines (36 loc) · 1.2 KB
/
utils.py
File metadata and controls
44 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import socket
import requests
import os
from log import logger
PLAINTEXT_FORMAT = "%s %s %s\n"
GRAPHITE_DOMAIN = "graphite.lain"
LAINLET_API_URL = "http://lainlet.lain:%s/v2/" % \
os.environ.get("LAINLET_PORT", "9001")
GRAPHITE_PORT = os.environ.get("GRAPHITE_PORT", "2003")
need_report = False
def report(key, value, timestamp):
if not need_report:
return
data = PLAINTEXT_FORMAT % (key, value, timestamp)
sock = None
try:
sock = socket.create_connection(
(GRAPHITE_DOMAIN, GRAPHITE_PORT), timeout=1)
sock.send(data)
except Exception as e:
logger.error("Send data to graphite failed: %s" % e)
finally:
if sock is not None:
sock.close()
def get_lain_config(key):
try:
resp = requests.get(LAINLET_API_URL + "configwatcher?target=" + key)
if resp.status_code == requests.codes.ok:
return resp.json()[key]
except Exception as e:
logger.error("Get config [%s] error: %s", key, e)
return None
def init_global_config():
global need_report
need_report = get_lain_config("dnshijack/" + GRAPHITE_DOMAIN) is not None
logger.info("Reporting to graphite is open: %s" % need_report)