UNPKG

4.39 kBJavaScriptView Raw
1'use strict';
2/**
3 *
4 */
5
6var debug = require('debug')('plugin:monitor');
7var cache = require('memored');
8const lynx = require('lynx');
9
10module.exports.init = function(config, logger, stats) {
11
12 var host = config.host || 'localhost';
13 var port = config.port || 8125;
14 var key = config.Requestkey || 'mg';
15
16 key = key + "." + os.hostname();
17 debug('grafana: ' + host + ':' + port);
18
19 const client = new lynx(host, port);
20
21 client.gauge(key + '_cpu', 0);
22 client.gauge(key + '_memory_rss', 0);
23 client.gauge(key + '_memory_heapUsed', 0);
24
25 var sc2xx = 'statusCode2xx';
26 var sc3xx = 'statusCode3xx';
27 var sc4xx = 'statusCode4xx';
28 var sc5xx = 'statusCode5xx';
29 var scNA = 'statusCodeNA';
30
31 clearCache();
32
33 function setGauge(statusCode) {
34 var tmp = 0;
35 if (statusCode >= 200 && statusCode < 300) {
36 cache.read(sc2xx, function(err, value) {
37 if (value) {
38 tmp = parseInt(value);
39 tmp ++;
40 cache.store(sc2xx, tmp.toString());
41 client.gauge(key + '_'+sc2xx, tmp);
42 } else {
43 cache.store(sc2xx, '1');
44 client.gauge(key + '_'+sc2xx, 1);
45 }
46 //debug('statusCode2xx ' + tmp);
47 });
48 } else if (statusCode >=300 && statusCode < 400) {
49 cache.read(sc3xx, function(err, value) {
50 if (value) {
51 tmp = parseInt(value);
52 tmp ++;
53 cache.store(sc3xx, tmp.toString());
54 client.gauge(key + '_'+sc3xx, tmp);
55 } else {
56 cache.store(sc3xx, 1);
57 client.gauge(key + '_'+sc3xx, 1);
58 }
59 //debug('statusCode3xx ' + tmp);
60 });
61 } else if (statusCode >=400 && statusCode < 500) {
62 cache.read(sc4xx, function(err, value) {
63 if (value) {
64 tmp = parseInt(value);
65 tmp ++;
66 cache.store(sc4xx, tmp.toString());
67 client.gauge(key + '_'+sc4xx, tmp);
68 } else {
69 cache.store(sc4xx, 1);
70 client.gauge(key + '_'+sc4xx, 1);
71 }
72 //debug('statusCode4xx ' + tmp);
73 });
74 } else if (statusCode >=500 && statusCode < 600) {
75 cache.read(sc5xx, function(err, value) {
76 if (value) {
77 tmp = parseInt(value);
78 tmp ++;
79 cache.store(sc5xx, tmp.toString());
80 client.gauge(key + '_'+sc5xx, tmp);
81 } else {
82 cache.store(sc5xx, 1);
83 client.gauge(key + '_'+sc5xx, 1);
84 }
85 //debug('statusCode5xx ' + value);
86 });
87 } else {
88 cache.read(scNA, function(err, value) {
89 if (value) {
90 cache.store(scNA, value ++);
91 client.gauge(key + '_'+scNA, value);
92 } else {
93 cache.store(scNA, 1);
94 client.gauge(key + '_'+scNA, 1);
95 }
96 //debug('statusCodeNA ' + tmp);
97 });
98 }
99 }
100
101 function clearCache() {
102 cache.store(sc2xx,'1');client.gauge(key + '_'+sc2xx, 0);
103 cache.store(sc3xx,'0');client.gauge(key + '_'+sc3xx, 0);
104 cache.store(sc4xx,'0');client.gauge(key + '_'+sc4xx, 0);
105 cache.store(sc5xx,'0');client.gauge(key + '_'+sc5xx, 0);
106 cache.store(scNA,'0');client.gauge(key + '_'+scNA, 0);
107 setTimeout(clearCache, 30000);
108 }
109
110 function sendEvent(statusCode) {
111 setGauge(statusCode);
112 //CPU and Memory
113 client.gauge(key + '_cpu', process.cpuUsage().user);
114 client.gauge(key + '_memory_rss', process.memoryUsage().rss);
115 client.gauge(key + '_memory_heapUsed', process.memoryUsage().heapUsed);
116 }
117
118 return {
119 onrequest: function(req, res, next) {
120 debug('plugin onrequest');
121 next();
122 },
123 onerror_request: function(req, res, data, next) {
124 debug('plugin onerror');
125 // Status Code
126 var statusCode = res.statusCode || -1;
127 sendEvent(statusCode);
128 next();
129 },
130 onend_request: function(req, res, data, next) {
131 debug('plugin onend_request');
132 next();
133 },
134 onend_response: function(req, res, data, next) {
135 debug('plugin onend_response');
136 // Status Code
137 var statusCode = res.statusCode || -1;
138 sendEvent(statusCode);
139 next();
140 }
141 };
142}