This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathG_Stat.py
126 lines (106 loc) · 5.55 KB
/
G_Stat.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
# Copyright 2013 II. Physikalisches Institut - Georg-August-Universitaet Goettingen
# Author: Christian Georg Wehrberger ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import hf
from sqlalchemy import TEXT, TIMESTAMP, Column
import re
from datetime import datetime
from BeautifulSoup import BeautifulSoup
class G_Stat(hf.module.ModuleBase):
config_keys = {
'source_html': ('HTML Source File', 'both||http://gstat2.grid.sinica.edu.tw/gstat/site/GoeGrid/bdii_site/bdii.goegrid.gwdg.de/all/'),
}
#config_hint = ''
table_columns = [
Column('source_html', TEXT),
], []
subtable_columns = {
'details_table': ([
Column('bdii_hostname', TEXT),
Column('service_name', TEXT),
Column('current_state', TEXT),
Column('information', TEXT),
Column('last_check', TIMESTAMP),
Column('error_info', TEXT)
], [])}
def prepareAcquisition(self):
# get urls from config and queue them for downloading
self.gstat_html = hf.downloadService.addDownload(self.config['source_html'])
self.details_table_db_value_list = []
def extractData(self):
data = {
'source_html': self.config['source_html'],
'status': 1
}
data_gstat_html = open(self.gstat_html.getTmpPath()).read().replace('\n',' ').replace('\r',' ')
if len(data_gstat_html) < 2:
data['status'] = -1
soup = BeautifulSoup(data_gstat_html)
# check whether there is a table
if False:
print('Error while parsing GStat HTML page: Could not find expected table.')
data['status'] = -1
else:
for table in soup.findAll('table'):
for tbody in table.findAll('tbody'):
detail = {}
for index_row, row in enumerate(tbody.findAll('tr')):
if index_row % 2 == 0: # regular row
for index_col, col in enumerate(row.findAll('td')):
if index_col == 0:
continue
elif index_col == 1:
detail['bdii_hostname'] = str(col.string).strip()
elif index_col == 2:
detail['service_name'] = str(col.string).strip()
elif index_col == 3:
for span in col.findAll('span'):
detail['current_state'] = str(span.string).strip()
elif index_col == 4:
detail['information'] = str(col.string).strip()
elif index_col == 5:
for script in col.findAll('script'):
time_seconds = re.findall(r'[0-9]+', str(script.string))
detail['last_check'] = datetime.fromtimestamp(int(time_seconds[0]))
self.details_table_db_value_list.append({})
self.details_table_db_value_list[index_row//2] = detail
else: # hidden row for error info
for col in row.findAll('td'):
for script in col.findAll('script'):
detail = {}
error_info = str(script.string[script.string.find('(\'')+2:script.string.find(';')-2])
#error_info = error_info.replace('%','%%')
error_info = error_info.replace('\\n','<br>')
detail['error_info'] = error_info
#print detail
#print index_row
self.details_table_db_value_list[index_row//2]['error_info'] = error_info
#print self.details_table_db_value_list
for detail in self.details_table_db_value_list:
if detail['current_state'].lower() == 'critical':
data['status'] = min(data['status'],0)
elif detail['current_state'].lower() == 'warning':
data['status'] = min(data['status'],0.5)
elif detail['current_state'].lower() == 'ok':
data['status'] = min(data['status'],1)
else:
data['status'] = min(data['status'],0)
return data
def fillSubtables(self, parent_id):
self.subtables['details_table'].insert().execute([dict(parent_id=parent_id, **row) for row in self.details_table_db_value_list])
def getTemplateData(self):
data = hf.module.ModuleBase.getTemplateData(self)
details = self.subtables['details_table'].select().where(self.subtables['details_table'].c.parent_id==self.dataset['id']).execute().fetchall()
data['details_gstat'] = map(dict, details)
return data