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