-
Notifications
You must be signed in to change notification settings - Fork 0
/
hresmon.py
executable file
·282 lines (256 loc) · 9.51 KB
/
hresmon.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
#!/usr/bin/env python
# Description
# This is the monitoring deamon that interacts with DB to write the monitored data per instance (VM or container). It exposes methods to create instance table after a instance is created,
# create reports on the instance resource usage.
#
# Each time an instance is created a new entry is added to the main table (resources) with NEW in the status. A new thread
# associated to this table is created in the compute node. The thread starts to read resource usage values and write them in a local table. Raw data measurements can be requested through irm-nova api
# for each instance
#
# Status
# - all implemented
#
#
#
import optparse, json, thread, ConfigParser, os, sqlite3, subprocess, time, multiprocessing, requests, datetime
from threading import Thread
import logging
import logging.handlers as handlers
from libnova import *
#from daemon import *
global myname, myprocesses, hresmonDbName
hresmonDbName = "hresmon.sqlite"
myname = os.path.basename(__file__)
TIMEOUT = 4
def createLogger():
global logger
logger = logging.getLogger("Rotating Log")
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s.%(msecs)d - %(levelname)s: %(filename)s - %(funcName)s: %(message)s', datefmt='%d/%m/%Y %H:%M:%S')
handler = handlers.TimedRotatingFileHandler(os.path.splitext(myname)[0]+".log",when="H",interval=24,backupCount=0)
## Logging format
handler.setFormatter(formatter)
if not logger.handlers:
logger.addHandler(handler)
# This function is exposed to the IRM-NOVA which adds entry whenever a VM needs to be monitored. Essentially is creating a new request to hresmon
def addResourceStatus(uuid,host,request,status):
print "In addResourceStatus",uuid,host,status
try:
db = sqlite3.connect(hresmonDbName)
db.execute('''INSERT INTO resources(uuid,HOST,REQUEST,status) VALUES (?,?,?,?)''',[uuid,host,request,status])
db.commit()
db.close
except sqlite3.Error, e:
error = {"message":e,"code":500}
logger.error(error)
return error
def updateResourceStatus (uuid,status):
print "In updateResourceStatus"
try:
db = sqlite3.connect(hresmonDbName)
cur = db.cursor()
cur.execute('''UPDATE resources SET status= :status WHERE uuid= :uuid''',{'status':status,'uuid':uuid})
db.commit()
db.close
except sqlite3.Error, e:
error = {"message":e,"code":500}
logger.error(error)
return error
# This function is exposed to the IRM-NOVA to check if the status of a monitoring instance
def checkResourceStatus(uuid):
print "In checkResourceStatus",uuid
try:
db = sqlite3.connect(hresmonDbName)
cur = db.cursor()
query = "SELECT status FROM resources WHERE uuid = \'"+uuid+"\'"
cur.execute(query)
[status] = cur.fetchone()
db.close
except TypeError, e:
warning = {"message":e,"code":"500"}
print warning
logger.warning(warning)
return warning
except sqlite3.Error, e:
error = {"message":e,"code":500}
logger.error(error)
return error
logger.info("Completed!")
return status
def deleteResourceStatus():
print "In deleteResourceStatus"
# this function creates an agent python file to a local or remote host and starts the agent
def createAgent(data,url):
print "In createAgent"
#print "url",url
logger.info("Called")
headers = {'content-type': 'application/json'}
try:
#print "url",url
r = requests.post('http://'+url+':12000/createAgent', data, headers=headers)
logger.info("response:"+json.dumps(r.json()))
except Exception.message, e:
response.status = 400
error = {"message":e,"code":response.status}
logger.error(error)
return error
except requests.exceptions.RequestException:
error = {"message":"socket error","code":"500"}
print error
logger.error(error)
return error
logger.info("Completed!")
return r
def destroyAgent(uuid):
logger.info("Called")
print "In destroyAgent"
headers = {'content-type': 'application/json'}
try:
url = getUrlbyUuid(uuid)
if url != "":
data = {"uuid":uuid}
jsondata = json.dumps(data)
#print jsondata
#print "url",url
r = requests.delete('http://'+url+':12000/terminateAgent', data=jsondata, headers=headers)
updateResourceStatus (uuid,"ENDED")
logger.info("response:"+json.dumps(r.json()))
else:
r = "No Agent for",uuid
print r
logger.warning(r)
except Exception.message, e:
response.status = 400
error = {"message":e,"code":response.status}
logger.error(error)
return error
except requests.exceptions.RequestException:
error = {"message":"RequestException","code":"500"}
print error
logger.error(error)
return error
logger.info("Completed!")
return r
def getUrlbyUuid(uuid):
logger.info("Called")
print "In getUrlbyUuid"
ip = ""
try:
db = sqlite3.connect(hresmonDbName)
cur = db.cursor()
query = "SELECT HOST FROM resources WHERE uuid = \'"+uuid+"\'"
cur.execute(query)
[ip] = cur.fetchone()
db.close
logger.info("Completed!")
except TypeError, e:
warning = {"message":e,"code":"500"}
print warning
logger.warning(warning)
return ""
except sqlite3.Error, e:
error = {"message":e,"code":500}
logger.error(error)
return error
return ip
def getResourceValueStore(req):
print "In getResourceValueStore"
logger.info("Called")
headers = {'content-type': 'application/json'}
result = {}
try:
url = getUrlbyUuid(req['ReservationID'])
jsondata = json.dumps(req)
r = requests.post('http://'+url+':12000/getResourceValueStore', data=jsondata, headers=headers)
result["Metrics"] = r.json()
#logger.info("response:"+json.dumps(result))
except Exception.message, e:
response.status = 400
error = {"message":e,"code":response.status}
logger.error(error)
return error
except Exception,e:
error = {"message":e,"code":"500"}
logger.error(error)
return error
except TypeError,e:
error = {"message":e,"code":"500"}
logger.error(error)
return error
logger.info("Completed!")
return result
def checkNewRequests():
print "In checkNewRequests"
while True:
try:
db = sqlite3.connect(hresmonDbName)
cursor = db.cursor()
cursor.execute('''SELECT * from resources WHERE status = "NEW"''')
all_rows = cursor.fetchall()
#print "all_rows",all_rows
for row in all_rows:
#print "timestamp",row[4]
now = datetime.datetime.now()
#print "now",now
then = datetime.datetime.strptime(row[4],"%Y-%m-%d %H:%M:%S")
tdelta = now - then
seconds = tdelta.total_seconds()
#print "tdelta",seconds
if seconds < TIMEOUT:
r = createAgent(row[2],row[1])
res = r.json()
if 'code' not in res:
updateResourceStatus(row[0],"RUNNING")
else:
updateResourceStatus(row[0],"ERROR")
#time.sleep(5)
db.close
time.sleep(0.5)
except AttributeError,e:
error = {"message":e,"code":"400"}
print error
logger.error(error)
#return error
except sqlite3.Error, e:
print e
error = {"message":e,"code":500}
logger.error(error)
#return error
# if there is not a DB one will be created with the resource-status table
def init():
createLogger()
try:
conn = sqlite3.connect(hresmonDbName)
conn.execute('''CREATE TABLE IF NOT EXISTS resources ("uuid" TEXT PRIMARY KEY NOT NULL UNIQUE , "HOST" TEXT DEFAULT False, "REQUEST" TEXT NOT NULL, "status" BOOL NOT NULL DEFAULT False, "Timestamp" DATE DEFAULT (datetime('now','localtime')))''')
conn.close()
except sqlite3.Error, e:
error = {"message":e,"code":500}
logger.error(error)
return error
# the daemon starts and checks the resources-status table constantly for new requests for monitoring by checking if the Status is NEW. If so, it calls the createAgent function and call updateResourceStatus to update the resource-status table relative entry with the name of the agent created and status to ACTIVE
def start():
print "In start"
# check if it's already running
myname = os.path.basename(__file__)
command = "ps -fe | grep "+myname+" | grep python | grep -v grep"
proccount = subprocess.check_output(command,shell=True).count('\n')
proc = subprocess.check_output(command,shell=True)
if proccount > 1:
error = "---Check if "+myname+" is already running. Connection error---"
print error
logger.error(error)
sys.exit(0)
else:
#t = multiprocessing.Process(name="monMaster",target=checkNewRequests,args=())
#t.daemon = True
#t.start()
#msg = "hresmon started"
#print msg
#while True:
# time.sleep(100)
checkNewRequests()
def main():
init()
start()
if __name__ == '__main__':
main()