-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.py
103 lines (86 loc) · 2.88 KB
/
common.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
"""
This script holds the common functions between the GOCDB and
APEL metric collecting scripts
"""
import logging
import json
import requests
from datetime import datetime, timedelta
from elasticsearch import Elasticsearch
logger = logging.getLogger(__name__)
class GetData(object):
"""This class is used to fetch data"""
def __init__(self, data, location, url):
self.data = data
self.location = location
self.url = url
def data_finder(self):
"""This function fetches the data"""
try:
info = \
self.location.getElementsByTagName(self.data.upper())[0]\
.firstChild.nodeValue
# finds the sitename and stores it
return info
except IndexError:
logger.warning('Index error when requesting '
+self.data.upper() + 'from' + self.url)
class ModLogger(object):
"""This class is used to modify the logger"""
def __init__(self, LogName):
self.LogName = LogName
def logger_mod(self):
"""Sets up the logger"""
# sets the logger
#logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# sets the a file handler
handler = logging.FileHandler(self.LogName)
handler.setLevel(logging.INFO)
# create a logging format
formatter = \
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s\
- %(message)s')
handler.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(handler)
logger.info('Hello world')
class ESWrite(object):
"""This class writes to elastic search"""
def __init__(self, dictionary):
self.dictionary = dictionary
self.elastic = Elasticsearch(
[
{
'host': 'elasticsearch1.gridpp.rl.ac.uk',
'port': 9200,
},
{
'host': 'elasticsearch5.gridpp.rl.ac.uk',
'port': 9200,
},
{
'host': 'elasticsearch6.gridpp.rl.ac.uk',
'port': 9200,
},
{
'host': 'elasticsearch7.gridpp.rl.ac.uk',
'port': 9200,
},
{
'host': 'elasticsearch8.gridpp.rl.ac.uk',
'port': 9200,
},
],
use_ssl=True,
verify_certs=False,
)
def write(self):
"""This function writes the data to elastic search"""
data = json.dumps(self.dictionary)
date = datetime.strftime(datetime.now(), '%Y.%m.%d')
self.elastic.index(
index="logstash-gridtools-metrics-%s" % date,
doc_type='doc',
body=data,
)