-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendtweet.py
executable file
·201 lines (194 loc) · 8.01 KB
/
sendtweet.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
from base import *
import time
import twitter
import traceback
import random
import datetime
from datetime import date as date
from datetime import datetime as dt
from datetime import timedelta as td
utcoffset = round( (( dt.now() - td(days=1) ) - ( dt.utcnow() - td(days=1) )).total_seconds() / 60 )
random.seed()
from twitter_auth import *
tapi = twitter.Api( consumer_key=consumer_key, consumer_secret=consumer_secret, access_token_key=access_token, access_token_secret=access_token_secret )
def sendtweet(msg):
if len(msg) > 280:
msg = msg[:280-3] + "..."
try:
tapi.PostUpdate( msg )
return True
except twitter.error.TwitterError as ex:
if ex.args[0][0]["code"] == 187:
# duplicate, not sending again
return None
# errs = ex.response_data.get("errors",[])
# for err in errs:
# if err.get("code",0) == 187:
# # duplicate, not sending again
# return None
traceback.print_exc(file=sys.stdout)
return False
if len(sys.argv) < 2:
pass
elif sys.argv[1] == "routermsg":
now = time.time()
last = mongodb["tweets"].find_one({},sort=[("ctime",-1)])
last = now - 3600 if last is None else last["ctime"]
for c in mongodb["changes"].find( { "$and":[ { "time": { "$gt": last } }, { "time": { "$lte": now } } ] }, sort = [("time",1)] ):
q = {
"param":c["param"],
"$and": [
{ "$or": [
{"old":{"$exists":False}},
{"old":c["old"]},
]},
{ "$or": [
{"new":{"$exists":False}},
{"new":c["new"]},
]},
{ "$or": [
{"modified":{"$exists":False}},
{"modified":{"$in": c.get("detail",{}).get("modified",[]) }},
]},
],
}
tpls = list(mongodb["templates"].find( q ))
if len(tpls) > 0:
msg = random.choice(tpls)["msg"].format_map(c)
if sendtweet(msg) is not False:
print(msg)
mongodb["tweets"].insert_one({ "msg": msg, "sent": now, "ctime": c["time"] })
break
elif sys.argv[1] == "netstate":
states = {}
for s in mongodb["nodes"].distinct("state"):
states[s] = mongodb["nodes"].count_documents({"state":s})
# states[s] = mongodb["nodes"].find({"state":s}).count()
states = sorted( states.items(), key = lambda x: x[1], reverse = True )
msg = "Zum Freifunk Potsdam Netzwerk gehören %d Router, davon sind" % sum([ x[1] for x in states ])
if len(states) > 1:
msg += " %d %s" % (states[0][1],states[0][0])
for s,nr in states[1:-1]:
msg += ", %d %s" % (nr,s)
msg += " und %d %s." % (states[-1][1],states[-1][0])
else:
msg += " alle %s." % states[0][0]
for s in sorted(mongodb["nodes"].distinct("state")):
if s != "online":
print( "%s:\n%s\n" % ( s, ", ".join([ n["hostname"] for n in mongodb["nodes"].find({"state":s}) ]) ) )
print()
print(msg)
sendtweet(msg)
users = {}
allusers = {}
q = 'SELECT mean(leases) \
FROM freifunk."default".dhcp \
WHERE time > NOW() - 48h AND time < NOW() - 2h \
GROUP BY time(5m),network,hostname fill(none)'
ires = influxdb.query(q)
for r in ires.get('results',[]):
for s in r.get('series',[]):
d = s.get('tags',{})
cols = s.get('columns',[])
vals = s.get('values',[])
if len(cols) * len(vals) > 0:
vals.sort(key=lambda x: x[cols.index('time')])
host = d["hostname"]
for v in vals:
time = v[cols.index('time')]
ltime = dt.strptime(time,"%Y-%m-%dT%H:%M:%SZ") + td( minutes=utcoffset )
if ltime.date() == date.today() - td(days=1):
u = float(v[cols.index('mean')])
if host not in users:
users[host] = {}
if time not in users[host]:
users[host][time] = {
"time": ltime.strftime("%H:%M"),
"users":0,
}
users[host][time]["users"] += u
if time not in allusers:
allusers[time] = {
"time": ltime.strftime("%H:%M"),
"users":0,
}
allusers[time]["users"] += u
allusers = sorted( allusers.values(), key = lambda x: x["users"] )
umin = allusers[0]
umax = allusers[-1]
urecords = []
for h in list(users.keys()):
r = sorted( users[h].values(), key = lambda x: x["users"] )[-1]
r["host"] = h
urecords.append( r )
urecords.sort( key = lambda x: x["users"], reverse = True )
rx = {}
tx = {}
allrx = 0
alltx = 0
q = 'SELECT sum("rx_bytes") \
FROM "oneyear"."traffic_rx" \
WHERE "device" =~ /(ffuplink|ffvpn)$/ AND time > NOW() - 48h AND time < NOW() - 2h \
GROUP BY time(1h),hostname fill(none)'
ires = influxdb.query(q)
for r in ires.get('results',[]):
for s in r.get('series',[]):
d = s.get('tags',{})
cols = s.get('columns',[])
vals = s.get('values',[])
if len(cols) * len(vals) > 0:
vals.sort(key=lambda x: x[cols.index('time')])
host = d["hostname"]
for v in vals:
time = v[cols.index('time')]
ltime = dt.strptime(time,"%Y-%m-%dT%H:%M:%SZ") + td( minutes=utcoffset )
if ltime.date() == date.today() - td(days=1):
tr = int(v[cols.index('sum')])
if host not in rx:
rx[host] = 0
rx[host] += tr
allrx += tr
q = 'SELECT sum("tx_bytes") \
FROM "oneyear"."traffic_tx" \
WHERE "device" =~ /(ffuplink|ffvpn)$/ AND time > NOW() - 48h AND time < NOW() - 2h \
GROUP BY time(1h),hostname fill(none)'
ires = influxdb.query(q)
for r in ires.get('results',[]):
for s in r.get('series',[]):
d = s.get('tags',{})
cols = s.get('columns',[])
vals = s.get('values',[])
if len(cols) * len(vals) > 0:
vals.sort(key=lambda x: x[cols.index('time')])
host = d["hostname"]
for v in vals:
time = v[cols.index('time')]
ltime = dt.strptime(time,"%Y-%m-%dT%H:%M:%SZ") + td( minutes=utcoffset )
if ltime.date() == date.today() - td(days=1):
tr = int(v[cols.index('sum')])
if host not in tx:
tx[host] = 0
tx[host] += tr
alltx += tr
traf = []
for h in set(rx.keys()) | set(tx.keys()):
traf.append(( h,rx.get(h,0) + tx.get(h,0) ))
traf.sort( key=lambda x: x[1], reverse = True )
msg = "Die meisten Clients waren gestern um %s online (%d), die wenigsten um %s (%d). " % \
( umax["time"], umax["users"], umin["time"], umin["users"] )
print(msg)
sendtweet(msg)
for urec in urecords[:3]:
msg = "Gestern um %s waren bei %s %d Clients online." % (urec["time"],urec["host"],urec["users"])
print(msg)
sendtweet(msg)
for h,tr in traf[:3]:
msg = "%s hat gestern %s übertragen, davon wurde %s heruntergeladen und %s hochgeladen." % \
(h, formatBytes(tr,2), formatBytes(rx[h],2), formatBytes(tx[h],2))
print(msg)
sendtweet(msg)
msg = "Insgesamt wurden gestern %s übertragen, davon wurden %s heruntergeladen und %s hochgeladen." % \
(formatBytes(allrx + alltx,2), formatBytes(allrx,2), formatBytes(alltx,2))
print(msg)
sendtweet(msg)