-
Notifications
You must be signed in to change notification settings - Fork 19
/
PrometheusDao.py
78 lines (61 loc) · 1.79 KB
/
PrometheusDao.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
# -*- coding: utf-8 -*-
from PrometheusConfig import filepath
import os
def write_file(filename, content):
if filepath.endswith('/'):
rule_file = filepath + filename + '.yml'
else:
rule_file = filepath + '/' + filename + '.yml'
try:
with open(rule_file, 'w') as f:
f.write(content)
return 0
except Exception, e:
print e
return 1
def update_write_file(filename, content, _name):
if filepath.endswith('/'):
rule_file = filepath + filename + '.yml'
_file = filepath + _name + '.yml'
else:
rule_file = filepath + '/' + filename + '.yml'
_file = filepath + '/' + _name + '.yml'
os.system("mv " + _file + ' ' + rule_file)
try:
with open(rule_file, 'w') as f:
f.write(content)
return 0
except Exception, e:
print e
return 1
def delete_file(_name):
if filepath.endswith('/'):
_file = filepath + _name + '.yml'
else:
_file = filepath + '/' + _name + '.yml'
try:
os.system("rm -rf " + _file)
return 0
except Exception, e:
print e
return 1
def get_rules():
rs = []
try:
for root, dirs, files in os.walk(filepath):
rs.append(files)
except Exception, e:
print e
return rs
def get_detail(filename):
try:
if filepath.endswith('/'):
rule_file = filepath + filename + '.yml'
else:
rule_file = filepath + '/' + filename + '.yml'
content = os.popen('head -1 ' + rule_file).read()
values = eval(content[1:len(content)])
return values
except Exception, e:
print e
return {'name': '', 'alert': '', 'expr': '', '_for': '', 'level': '', 'summary': '', 'description': ''}