-
Notifications
You must be signed in to change notification settings - Fork 0
/
ahiruyaki_counter.py
312 lines (283 loc) · 10.3 KB
/
ahiruyaki_counter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# coding: UTF-8
import os
import sys
import json
import re
import urllib2
import datetime
import time
import ConfigParser
import socket
import struct
import string
import tweepy
class ZabbixSender:
zbx_header = 'ZBXD'
zbx_version = 1
zbx_sender_data = {u'request': u'sender data', u'data': []}
send_data = ''
def __init__(self, server_host, server_port = 10051):
self.server_ip = socket.gethostbyname(server_host)
self.server_port = server_port
def AddData(self, host, key, value, clock = None):
add_data = {u'host': host, u'key': key, u'value': value}
if clock != None:
add_data[u'clock'] = clock
self.zbx_sender_data['data'].append(add_data)
return self.zbx_sender_data
def ClearData(self):
self.zbx_sender_data['data'] = []
return self.zbx_sender_data
def __MakeSendData(self):
zbx_sender_json = json.dumps(self.zbx_sender_data, separators=(',', ':'), ensure_ascii=False).encode('utf-8')
json_byte = len(zbx_sender_json)
self.send_data = struct.pack("<4sBq" + str(json_byte) + "s", self.zbx_header, self.zbx_version, json_byte, zbx_sender_json)
def Send(self):
self.__MakeSendData()
so = socket.socket()
so.connect((self.server_ip, self.server_port))
wobj = so.makefile(u'wb')
wobj.write(self.send_data)
wobj.close()
robj = so.makefile(u'rb')
recv_data = robj.read()
robj.close()
so.close()
tmp_data = struct.unpack("<4sBq" + str(len(recv_data) - struct.calcsize("<4sBq")) + "s", recv_data)
recv_json = json.loads(tmp_data[3])
return recv_data
class ZabbixAPI(object):
# ZABBIX Server APIのURL
zbx_url = ""
# APIを利用するユーザーID
zbx_userid = ""
# パスワード
zbx_passwd = ""
#認証キー
zbx_auth = ""
# HTTPHEADER
headers = {"Content-Type":"application/json-rpc"}
# グラフサイズ width:800
zbx_gwidth = "800"
# グラフサイズ height:300
zbx_gheight = "300"
# グラフ枠線 デフォルト:なし
zbx_gborder = "0"
# auth key 発行用関数
# 戻り値:ZABBIX API auth key
def auth(self):
auth_post = json.dumps({
'jsonrpc': '2.0',
'method': 'user.login',
'params': {
'user': self.zbx_userid,
'password': self.zbx_passwd},
'auth':None,
'id': 1})
#opener = urllib2.build_opener(urllib2.HTTPSHandler())
#urllib2.install_opener(opener)
req = urllib2.Request(self.zbx_url, auth_post, self.headers)
f = urllib2.urlopen(req)
str_value = f.read()
f.close()
value = json.loads(str_value)
try:
self.zbx_auth = value["result"]
return value["result"]
except:
print "Authentication failure"
return 0
quit()
storage.close()
def send(self, json_data):
req = urllib2.Request(self.zbx_url, json_data, self.headers)
f = urllib2.urlopen(req)
str_value = f.read()
f.close()
dict_value = json.loads(str_value)
return dict_value
# cokkie 取得用login関数
# 戻り値:cokkieに入れる認証トークン
def login(self, user, passwd):
json_login = json.dumps({
"jsonrpc":"2.0",
"method":"user.login",
"params":{
"user":user,
"password":passwd},
"id":1})
sessionid = self.send(json_login)
cookie = sessionid["result"]
cookie = 'zbx_sessionid=' + cookie
return cookie
# グラフ取得
def get_graph(self, cookie, graphid, period, stime):
opener = urllib2.build_opener()
opener.addheaders.append(("cookie",cookie))
graph_url = self.zbx_url.replace("api_jsonrpc", "chart2")
graphi_get_url = "%s?graphid=%s&width=%s&height=%s&border=%s&period=%s&stime=%s" % (
graph_url,
graphid,
self.zbx_gwidth,
self.zbx_gheight,
self.zbx_gborder,
period,
stime)
graph = opener.open(graphi_get_url)
return graph
def run_zbxapi(reqjson):
returndata = zbx_api.send(reqjson)
result = returndata["result"]
if len(result) == 1:
return result
else:
print "error", reqjson, result
exit()
def authorize(conf):
""" Authorize using OAuth.
"""
auth = tweepy.OAuthHandler(conf.get("twitter","consumer_key"), conf.get("twitter","consumer_secret"))
auth.set_access_token(conf.get("twitter","access_key"), conf.get("twitter","access_secret"))
return auth
def create_zbx_item(tweetid, zbx_api, zbx_auth_key, base_item_key):
item_key = base_item_key + tweetid
reqdata = json.dumps({
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"hostids": "10107",
"search": {
"key_": item_key}
},
"auth":zbx_auth_key,
"id": 1})
zbx_item_check_result = zbx_api.send(reqdata)
if len(zbx_item_check_result["result"]) == 0:
if base_item_key.find("hcount") > -1:
attweetid = u"[毎時]@" + tweetid
applications_id = ["461"]
else:
attweetid = u"[日次]@" + tweetid
applications_id = ["462"]
reqdata = json.dumps({
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"name": attweetid,
"key_": item_key,
"hostid": "10107",
"type": 2,
"value_type": 3,
"applications":
applications_id
,
},
"auth":zbx_auth_key,
"id": 1})
zbx_item_create_result = zbx_api.send(reqdata)
return zbx_item_create_result
else:
return zbx_item_check_result
def put_zbx_sender(zbxsvip, zbx_key, hostip, sendvalue):
sender = ZabbixSender(zbxsvip)
sender.AddData(hostip, zbx_key, sendvalue)
try:
sender.Send()
except:
print "[ERROR] host: %s value: %s"%(hostip,sendvalue)
sender.ClearData()
def get_zbx_ahiruyaki_item(zbx_api, zbx_auth_key, item_key):
reqdata = json.dumps({
"jsonrpc": "2.0",
"method": "item.get",
"params": {
"hostids": "10107",
"search": {
"key_": item_key}
},
"auth":zbx_auth_key,
"id": 1})
return zbx_api.send(reqdata)
if __name__ == '__main__':
base = os.path.dirname(os.path.abspath(__file__))
config_file_path = os.path.normpath(os.path.join(base, 'config.ini'))
conf = ConfigParser.SafeConfigParser()
conf.read(config_file_path)
# zabbix api login
zbx_api = ZabbixAPI()
zbx_api.zbx_url = conf.get("zabbix","url")
zbx_api.zbx_userid = conf.get("zabbix","userid")
zbx_api.zbx_passwd = conf.get("zabbix","passwd")
# get zabbxi api cookie
zbx_auth_key = zbx_api.auth()
argvs = sys.argv
print argvs[1]
if len(argvs) == 2 and argvs[1] == "day":
base_item_key = "ahiruyaki.dcount."
oneoldtime = datetime.datetime.utcnow() - datetime.timedelta(days = 1)
start_time = datetime.datetime(
int(oneoldtime.strftime("%Y")),
int(oneoldtime.strftime("%m")),
int(oneoldtime.strftime("%d")),
0,0,0,0)
end_time = datetime.datetime(
int(oneoldtime.strftime("%Y")),
int(oneoldtime.strftime("%m")),
int(oneoldtime.strftime("%d")),
23,59,59,999999)
elif len(argvs) == 2 and argvs[1] == "hour":
base_item_key = "ahiruyaki.hcount."
oneoldtime = datetime.datetime.utcnow() - datetime.timedelta(hours = 1)
start_time = datetime.datetime(
int(oneoldtime.strftime("%Y")),
int(oneoldtime.strftime("%m")),
int(oneoldtime.strftime("%d")),
int(oneoldtime.strftime("%H")),
0,0,0)
end_time = datetime.datetime(
int(oneoldtime.strftime("%Y")),
int(oneoldtime.strftime("%m")),
int(oneoldtime.strftime("%d")),
int(oneoldtime.strftime("%H")),
59,59,999999)
else:
print len(argvs)
print "Error"
twdate = start_time + datetime.timedelta(hours = 9)
# print start_time + datetime.timedelta(hours = 9)
# print end_time + datetime.timedelta(hours = 9)
use_zbx_item = get_zbx_ahiruyaki_item(zbx_api, zbx_auth_key, base_item_key)
yakishi_list = {}
for item in use_zbx_item["result"]:
twname = item["key_"].replace(base_item_key, "")
yakishi_list[twname] = 0
postdata = unicode(twdate.strftime("%Y年%m月%d日%H時台に焼かれたあひるの数\n(テスト運用中)\n"),'utf-8', 'ignore')
auth = authorize(conf)
api = tweepy.API(auth_handler=auth)
keywords = [u"あひる焼き OR #あひる焼き OR Ahiruyaki OR #Ahiruyaki ", u"-RT"]
query = ' AND '.join(keywords)
new_yaskihi_list = []
for tweet in api.search(q=query, count=1000):
textdata = tweet.text.encode('utf-8')
if textdata.find("あひる焼き") != -1 and textdata.find("あひる焼きカウンター") == -1:
if start_time < tweet.created_at < end_time :
if not tweet.user.screen_name in yakishi_list:
itemdata = create_zbx_item(tweet.user.screen_name, zbx_api, zbx_auth_key, base_item_key)
new_yaskihi_list.append(tweet.user.screen_name)
yakishi_list[tweet.user.screen_name] = 1
else:
yakishi_list[tweet.user.screen_name] += 1
time.sleep(60)
for id in new_yaskihi_list:
item_key = base_item_key + id
put_zbx_sender(conf.get("zabbix","ip"), item_key, "ahiruyaki", 0)
if len(yakishi_list) == 0:
postdata = postdata + u"あひるは焼かれなかった\n"
else:
for id, count in yakishi_list.items():
item_key = base_item_key + id
put_zbx_sender(conf.get("zabbix","ip"), item_key, "ahiruyaki", count)
postdata = postdata + id + ": " + str(count) + u"焼き\n"
#post twitter
# print postdata
# api.update_status(postdata)