-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnodejs.py
More file actions
126 lines (101 loc) · 3.93 KB
/
nodejs.py
File metadata and controls
126 lines (101 loc) · 3.93 KB
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
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
"""
Collectd Python plugin to get nodejs
"""
import collectd
import ast
import json
import time
import requests
from utils import *
HTTP_STATS = "httpstats"
SYS_DETAILS = "systemstats"
docs = [HTTP_STATS, SYS_DETAILS]
class Nodejs():
def __init__(self):
self.host = None
self.hosts = []
self.conn = None
self.cur = None
self.status = {}
self.previousData = {}
self.port = 8080
self.first_poll = {}
self.interval = None
self.version = None
self.last_read_bucket_index = None
# Instantiating the status variables for all the hosts during init
def read_config(self, cfg):
for children in cfg.children:
if children.key == INTERVAL:
self.interval = children.values[0]
if children.key == PORT:
self.port = children.values[0]
@staticmethod
def add_common_params(result_dict, doc_type):
hostname = gethostname()
timestamp = int(round(time.time() * 1000))
result_dict[PLUGIN] = "nodejs"
result_dict[HOSTNAME] = hostname
result_dict[TIMESTAMP] = timestamp
result_dict[PLUGINTYPE] = doc_type
result_dict["_documentType"] = doc_type
result_dict[ACTUALPLUGINTYPE] = "nodejs"
collectd.info("Plugin nodejs: Added common parameters successfully")
@staticmethod
def dispatch_data(result_dict):
collectd.info("Plugin nodejs: Values dispatched = " + json.dumps(result_dict))
dispatch(result_dict)
def poll(self):
node_stats = dict()
last_read_bucket_index = self.last_read_bucket_index
try:
# node_stats = self.get_node_http_stats()
url = "http://localhost:{}/swagger-stats/stats?fields=timeline".format(self.port)
collectd.info("Request URL")
response = requests.get(url)
if response.status_code == 200:
collectd.info('Response code 200 received')
content = response.content
#response_json = json.loads(content)
response_json = ast.literal_eval(content)
timeline_stats = response_json.get("timeline")
bucket_current = timeline_stats.get("settings").get("bucket_current")
if not last_read_bucket_index and bucket_current:
last_read_bucket_index = bucket_current - 1
else:
last_read_bucket_index += 1
if last_read_bucket_index :
if(timeline_stats.get("data").get(str(last_read_bucket_index))):
last_value = timeline_stats.get("data").get(str(last_read_bucket_index))
http_stats = last_value.get("stats")
node_stats[HTTP_STATS] = http_stats
sys_stats = last_value.get("sys")
node_stats[SYS_DETAILS] = sys_stats
except Exception as ex:
collectd.error('Error collecting nodejs application stats : %s ' % ex.message)
return node_stats
def read(self):
result_dicts = self.poll()
if not result_dicts:
collectd.error("Plugin nodejs: Unable to fetch information ")
else:
collectd.info("Plugin nodejs: Success fetching information")
for doc in docs:
result_dict = result_dicts[doc]
self.add_common_params(result_dict, doc)
# dispatch data to collectd
self.dispatch_data(result_dict)
def read_temp(self):
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
def init(self):
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
obj = Nodejs()
collectd.register_config(obj.read_config)
collectd.register_read(obj.read_temp)