-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathli_metrics_agent.py
1010 lines (868 loc) · 36.3 KB
/
li_metrics_agent.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding=utf-8
"""
Copyright 2012 Load Impact
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.
"""
from __future__ import division
import base64
import codecs
import ConfigParser
# set_tunnel() is not supported in Python26
import json
import logging
import logging.config
import logging.handlers
import math
import optparse
import os
import platform
import Queue
import re
import signal
import subprocess
import sys
import threading
import time
import traceback
if sys.version_info < (2,7):
import httplib27 as httplib
import socket27 as socket
else:
import httplib
import socket
running_on_linux = sys.platform.startswith('linux')
if running_on_linux:
import resource
from collections import defaultdict
from datetime import datetime
from urlparse import urlparse
try:
import psutil
except ImportError:
print "can't find module psutil"
sys.exit(1)
__author__ = "Load Impact"
__copyright__ = "Copyright (c) 2012, Load Impact"
__license__ = "Apache License v2.0"
__version__ = "1.1.1"
__email__ = "[email protected]"
frozen = getattr(sys, 'frozen', '')
if not frozen:
# regular python
PROGRAM_DIR = os.path.dirname(os.path.realpath(__file__))
else:
# running in py2exe
PROGRAM_DIR = os.path.dirname(os.path.realpath(sys.executable))
CONFIG_FILE = os.path.join(PROGRAM_DIR, 'li_metrics_agent.conf')
PANIC_LOG_FILENAME = 'li_metrics_panic.log'
if sys.platform.startswith('linux'):
PANIC_LOG_PATH = '/var/log/%s' % (PANIC_LOG_FILENAME)
else:
PANIC_LOG_PATH = os.path.join(PROGRAM_DIR, PANIC_LOG_FILENAME)
PROTOCOL_VERSION = "1"
PLATFORM_STRING = platform.platform()
AGENT_USER_AGENT_STRING = ("LoadImpactServerMetricsAgent/%s "
"(Load Impact; http://loadimpact.com);"
% __version__)
CONFIG_CMD_ARGS_REGEX = re.compile(r'( |"[^"]*?"|\'[^\']*?\')')
PERF_DATA_OPTS_REGEX = re.compile(r'''
(?:([^:\'" ]+)|(?:(?:[\'"]?)([^:\'"]+)(?:[\'"]?))) # Label
(?::([a-zA-Z%/]+))? # Unit
(?:[ ]*) # Multi-value separator
''', re.X)
NAGIOS_PERF_DATA_REGEX = re.compile(r'''
(?:[\'"]?)([^=\'"]+)(?:[\'"]?) # Label
=
([\d\.]+) # Value
([a-zA-Z%/]+)? # Unit
(?:;([\d\.]+)?)? # Warning level
(?:;([\d\.]+)?)? # Critical level
(?:;([\d\.]+)?)? # Min
(?:;([\d\.]+)?)? # Max
(?:[, ]*) # Multi-value separator
''', re.X)
DEFAULT_SERVER_METRICS_API_URL = 'http://api.loadimpact.com/v2/server-metrics'
DEFAULT_POLL_RATE = 30
DEFAULT_SAMPLING_INTERVAL = 3
DEFAULT_DATA_PUSH_INTERVAL = 10
UMASK = 0
WORK_DIR = "/"
MAX_FD = 1024
PID_FILE = "/var/run/li_metrics_agent.pid"
def panic_log(path, msg):
with open(path, 'a') as f:
f.write("%s - %s\n" % (datetime.now().isoformat(' '), msg))
def init_logging():
try:
logging.config.fileConfig(CONFIG_FILE)
except ConfigParser.NoSectionError, e:
# We ignore any parsing error of logging configuration variables.
pass
except Exception, e:
# Parsing of logging configuration failed, print something to a panic
# file in /var/log (Linux) or [Program] directory (Windows).
try:
panic_log(PANIC_LOG_PATH,
"failed parsing logging configuration: %s" % repr(e))
except IOError:
try:
panic_log(PANIC_LOG_FILENAME,
"failed parsing logging configuration: %s" % repr(e))
except IOError:
print "failed parsing logging configuration: %s" % repr(e)
sys.exit(1)
def daemonize():
"""Code copied from:
http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
Copyright (C) 2005 Chad J. Schroeder
Licensed under the PSF License
"""
if hasattr(os, "devnull"):
REDIRECT_TO = os.devnull
else:
REDIRECT_TO = "/dev/null"
try:
pid = os.fork()
except OSError, e:
raise Exception("%s [%d]" % (e.strerror, e.errno))
if pid == 0:
os.setsid()
signal.signal(signal.SIGHUP, signal.SIG_IGN)
try:
pid = os.fork()
except OSError, e:
raise Exception("%s [%d]" % (e.strerror, e.errno))
if pid == 0:
os.chdir(WORK_DIR)
os.umask(UMASK)
else:
os._exit(0)
else:
os._exit(0)
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if maxfd == resource.RLIM_INFINITY:
maxfd = MAX_FD
for fd in range(0, maxfd):
try:
os.close(fd)
except OSError:
pass
os.open(REDIRECT_TO, os.O_RDWR)
os.dup2(0, 1)
os.dup2(0, 2)
return 0
def log_dump():
"""Dump stack trace (one frame per line) to log."""
tb = str(traceback.format_exc()).split("\n")
logging.error("")
for i, a in enumerate(tb):
if a.strip():
logging.error(a)
def check_output(*popenargs, **kwargs):
"""Based on check_output in Python 2.7 subprocess module."""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
"""https://github.com/pyinstaller/pyinstaller/wiki/Recipe-subprocess"""
if running_on_linux:
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
else:
kwargs.pop('stdin', None)
kwargs.pop('stderr', None)
process = subprocess.Popen(stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
return output, retcode
class AgentState(object):
"""The agent can only be in these states. Either sending data or not."""
IDLE = 1
ACTIVE = 2
@staticmethod
def get_name(state):
return 'IDLE' if AgentState.IDLE == state else 'ACTIVE'
@staticmethod
def is_valid(state):
return True if state in [AgentState.IDLE, AgentState.ACTIVE] else False
class PsutilAdapter(object):
"""Adapter for psutil methods calls depends on psutil current version"""
@staticmethod
def normalized_version():
# psutil versions are simple integers as x.x.x
v = psutil.__version__
return tuple(map(int, (v.split("."))))
@classmethod
def version_gte_than(self, version_tuple):
return self.normalized_version() >= version_tuple
@classmethod
def net_io_counters(self, pernic=False):
if self.version_gte_than((1,0,0)):
return psutil.net_io_counters(pernic=pernic)
else:
return psutil.network_io_counters(pernic=pernic)
class NagiosPluginExitCode(object):
"""Enum mapping process exit codes to Nagios service states."""
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
@staticmethod
def get_name(exit_code):
if exit_code == NagiosPluginExitCode.OK:
return 'OK'
if exit_code == NagiosPluginExitCode.WARNING:
return 'WARNING'
elif exit_code == NagiosPluginExitCode.CRITICAL:
return 'CRITICAL'
elif exit_code == NagiosPluginExitCode.UNKNOWN:
return 'UNKNOWN'
return 'INVALID'
class ApiClient(object):
"""An API HTTP client class that has two states, poll and active. In active
state a TCP connection will be kept alive across requests where as in poll
state it will not.
"""
def __init__(self, agent_name, token, api_url, proxy_url=None):
self.agent_name = agent_name
self.token = token
self.parsed_api_url = urlparse(api_url)
self.parsed_proxy_url = urlparse(proxy_url) if proxy_url else None
self.state = AgentState.IDLE
self.conn = None
self.lock = threading.Lock()
def _build_auth(self, username, password=''):
return 'Basic %s' % base64.b64encode('%s:%s' % (username, password))
def _connect(self):
if not self.conn:
scheme = (self.parsed_proxy_url.scheme if self.parsed_proxy_url
else self.parsed_api_url.scheme)
host = (self.parsed_proxy_url.hostname if self.parsed_proxy_url
else self.parsed_api_url.hostname)
port = (self.parsed_proxy_url.port if self.parsed_proxy_url
else self.parsed_api_url.port)
if 'http' == scheme:
port = port if port else 80
self.conn = httplib.HTTPConnection(host, port=port)
else:
port = port if port else 443
self.conn = httplib.HTTPSConnection(host, port=port)
if self.parsed_proxy_url:
host = self.parsed_api_url.hostname
if 'http' == self.parsed_api_url.scheme:
port = (self.parsed_api_url.port if self.parsed_api_url.port
else 80)
else:
port = (self.parsed_api_url.port if self.parsed_api_url.port
else 443)
headers = {}
if self.parsed_proxy_url.username:
username = self.parsed_proxy_url.username
password = self.parsed_proxy_url.password
headers['Proxy-Authentication'] = self._build_auth(username,
password)
self.conn.set_tunnel(host, port=port, headers=headers)
def _close(self):
if self.conn:
self.conn.close()
self.conn = None
def _request(self, method, data=None, headers=None):
with self.lock:
try:
self._connect()
except socket.gaierror:
raise
except httplib.HTTPException:
raise
headers = {} if not isinstance(headers, dict) else headers
if 'Authorization' not in headers:
headers['Authorization'] = self._build_auth(self.token)
headers['User-Agent'] = AGENT_USER_AGENT_STRING
try:
self.conn.request(method, self.parsed_api_url.path, data,
headers)
resp = self.conn.getresponse()
ret = (resp.status, resp.read())
except httplib.HTTPException:
self._close()
raise
except socket.error:
self._close()
raise
# Close connection if in IDLE mode otherwise leave open.
if AgentState.IDLE == self.state:
self._close()
return ret
def poll(self):
data = {
'name': self.agent_name,
'version': PROTOCOL_VERSION,
'version_agent': __version__,
'os': PLATFORM_STRING
}
logging.debug(json.dumps(data))
return self._request('POST',
headers={'Content-Type': 'application/json'},
data=json.dumps(data))
def push_batch(self, batch):
data = []
for x in batch:
metric = {
'name': self.agent_name,
'version': PROTOCOL_VERSION,
'version_agent': __version__,
'label': x[0],
'min': x[1],
'max': x[2],
'avg': x[3],
'stddev': x[4],
'median': x[5],
'count': x[6],
'unit': x[7],
'warning_level': x[8],
'critical_level': x[9],
'lower_limit': x[10],
'upper_limit': x[11]
}
data.append(metric)
logging.debug(json.dumps(data))
return self._request('PUT',
headers={'Content-Type': 'application/json'},
data=json.dumps(data))
class Scheduler(object):
"""Scheduler is responsible for managing task threads. Each metric plugin
has its own task.
"""
def __init__(self):
self.tasks = []
def __repr__(self):
rep = ''
for task in self.tasks:
rep += '%s\n' % repr(task)
return rep
def add_task(self, task):
task.daemon = True
self.tasks.append(task)
def start(self):
for task in self.tasks:
logging.debug('starting %s', task)
task.start()
def stop(self, timeout=3):
for task in self.tasks:
logging.debug('stopping %s', task)
task.stop()
task.join(timeout=timeout)
class Reporting(threading.Thread):
"""
Thread responsible for sending result data back to the API.
"""
def __init__(self, queue, client):
threading.Thread.__init__(self)
self.queue = queue
self.client = client
self.running = True
def stop(self):
self.running = False
def run(self):
while self.running:
batch = []
# Empty queue
while True:
try:
data = self.queue.get_nowait()
batch.append(data)
except Queue.Empty:
break
if len(batch):
status, body = self.client.push_batch(batch)
if 201 != status:
logging.error("%d status code returned when pushing data "
"to server: \"%s\"" % (status, repr(body)))
time.sleep(2)
class Task(threading.Thread):
"""A task thread is responsible for collection and reporting of a single
metric. The metric can be one of the built-in ones or a Nagios-compatible
plugin that is executed as a sub-process.
"""
def __init__(self, queue, client, cmd, perf_data_opts, sampling_interval,
data_push_interval):
threading.Thread.__init__(self)
self.queue = queue
self.client = client
self.cmd = cmd
self.perf_data_opts = perf_data_opts
self.sampling_interval = sampling_interval
self.data_push_interval = data_push_interval
self.running = True
self.state = AgentState.IDLE
self.buffer = defaultdict(list)
self.prev_sent = 0
self.prev_recv = 0
self.last_push = time.time()
def __repr__(self):
return 'task %s' % (self.cmd)
def _next_line(self):
output, retcode = check_output(self.cmd, shell=True)
if retcode in [NagiosPluginExitCode.WARNING,
NagiosPluginExitCode.CRITICAL,
NagiosPluginExitCode.UNKNOWN]:
logging.warning("plugin \"%s\" exited with code other than OK: %d "
"(%s)" % (self.cmd, retcode,
NagiosPluginExitCode.get_name(retcode)))
return output
def _prepare_data(self, label):
count = len(self.buffer[label])
if count == 0:
return (0, 0, 0, 0, 0, 0)
# avg
vmin = sys.float_info.max
vmax = 0.0
total = 0.0
for v in self.buffer[label]:
vmin = min(vmin, v)
vmax = max(vmax, v)
total = total + v
avg = total / count
# std dev
total = 0.0
for v in self.buffer[label]:
total += ((v - avg) ** 2)
stddev = 0.0
if count > 1:
stddev = math.sqrt((1.0 / (count - 1)) * total)
# median
values = sorted(self.buffer[label])
if count % 2 == 1:
median = values[int((count + 1) / 2 - 1)]
else:
lower = values[int(count / 2 - 1)]
upper = values[int(count / 2)]
median = (float(lower + upper)) / 2
return (vmin, vmax, avg, stddev, median, count)
def push_data(self, perf_data):
for label, data in perf_data.iteritems():
self.buffer[label].append(float(data[1]))
if (self.last_push + self.data_push_interval) < time.time():
try:
for label, data in perf_data.iteritems():
label, value, unit, warning_level, critical_level, min_, max_ = data
aggregated_data = self._prepare_data(label)
data = (label,) + aggregated_data + (unit, warning_level,
critical_level, min_, max_)
try:
self.queue.put_nowait(data)
except Queue.Full:
pass # Ignore data
self.buffer[label] = []
except Exception:
log_dump()
self.last_push = time.time()
def run(self):
execution_time = time.time()
while self.running:
start = time.time()
try:
if AgentState.ACTIVE == self.state:
logging.debug('running %s ', self.cmd)
line = self._next_line()
try:
human_str, perf_data_str = line.split('|')
except ValueError:
perf_data_str = ''
perf_data_str = perf_data_str.strip()
perf_data = {}
for match in NAGIOS_PERF_DATA_REGEX.finditer(perf_data_str):
perf_data[match.group(1).strip()] = (match.group(1),
match.group(2),
match.group(3),
match.group(4),
match.group(5),
match.group(6),
match.group(7))
if len(perf_data):
if self.perf_data_opts and len(self.perf_data_opts) > 0:
for label in perf_data.keys():
if label in self.perf_data_opts:
unit = self.perf_data_opts[label]
if unit and not perf_data[label][2]:
perf_data[label][2] = unit
else:
del perf_data[label]
self.push_data(perf_data)
except Exception:
log_dump()
execution_time += self.sampling_interval
time.sleep(max(0.5, execution_time - start))
def set_state(self, state):
self.state = state
def stop(self):
self.running = False
class BuiltinMetricTask(Task):
def _next_line(self):
unknown_line = "unknown 0|unknown=0%;"
args = [s for s in re.split(r'( |".*?"|\'.*?\')', self.cmd)
if s.strip()]
if len(args) < 1:
logging.error('missing argument(s) for BUILTIN: ' + self.cmd)
return unknown_line
line = self._next_line_builtin(args)
return line if line else unknown_line
def _next_line_builtin(self, args):
raise NotImplementedError
class CPUMetricTask(BuiltinMetricTask):
"""Built-in metric task to measure CPU utilization %."""
def _next_line_builtin(self, args):
if len(args) > 1:
cpu_index = int(args[1], 10)
cpu = psutil.cpu_percent(interval=1, percpu=True)
try:
cpu = cpu[cpu_index]
return "CPU %d load %s%%|CPU %d=%s%%;" % (cpu_index, cpu,
cpu_index, cpu)
except IndexError:
logging.error("incorrect CPU index: %d" % cpu_index)
return None
else:
cpu = psutil.cpu_percent(interval=1)
return "CPU load %s%%|CPU=%s%%;" % (cpu, cpu)
class MemoryMetricTask(BuiltinMetricTask):
"""Built-in metric task to measure memory utilization %."""
def _next_line_builtin(self, args):
if hasattr(psutil, 'virtual_memory'):
mem = psutil.virtual_memory()
else:
mem = psutil.phymem_usage() # Deprecated in psutil 0.3.0 and 0.6.0
return "Memory usage %s%% |Memusage=%s%%;" % (mem.percent,
mem.percent)
class RateBasedMetrics(BuiltinMetricTask):
"""Base class for built-in metric tasks that are rate-based."""
def __init__(self, *args, **kwargs):
super(RateBasedMetrics, self).__init__(*args, **kwargs)
self.prev = defaultdict(int)
def _calculate_total(self, current, prev):
total = 0
p = self.prev[prev]
if p > 0:
total = current - p
self.prev[prev] = current
return total
def _calculate_total2(self, sent, prev_sent, recv, prev_recv):
total = 0
prevs = self.prev[prev_sent]
prevr = self.prev[prev_recv]
if prevs > 0:
total = (sent - prevs) + (recv - prevr)
self.prev[prev_sent] = sent
self.prev[prev_recv] = recv
return total
class NetworkMetricTask(RateBasedMetrics):
"""Built-in metric task to measure network utilization metrics:
- Bps (total, in and out)
- Packets/s (total, in and out)
"""
def _next_line_builtin(self, args):
valid_metrics = ['bps', 'bps-in', 'bps-out', 'pps', 'pps-in', 'pps-out']
interface = ""
if len(args) > 1 and args[1].lower() not in valid_metrics:
interface = args[1].replace("'", "")
counters = PsutilAdapter.net_io_counters(pernic=True)
try:
counters = counters[interface]
except KeyError:
logging.error("incorrect network interface name: "
"\"%s\"" % interface)
return None
# Format for label name in the report line below
interface = "_" + interface
else:
counters = PsutilAdapter.net_io_counters(pernic=False)
metric = 'bps'
metric_index = 2 if interface else 1
if len(args) > metric_index:
metric = args[metric_index].lower()
if metric not in valid_metrics:
metric = 'bps'
if metric == 'bps':
total = self._calculate_total2(counters.bytes_sent, 'bytes_sent',
counters.bytes_recv, 'bytes_recv')
elif metric == 'bps-in':
total = self._calculate_total(counters.bytes_recv, 'bytes_recv')
elif metric == 'bps-out':
total = self._calculate_total(counters.bytes_sent, 'bytes_sent')
elif metric == 'pps':
total = self._calculate_total2(counters.packets_sent,
'packets_sent',
counters.packets_recv,
'packets_recv')
elif metric == 'pps-in':
total = self._calculate_total(counters.packets_recv, 'packets_recv')
elif metric == 'pps-out':
total = self._calculate_total(counters.packets_sent, 'packets_sent')
metric_ps = total / self.sampling_interval / 1024
line = ("%s over %s sec|Network%s=%.2f%s"
% (total, self.sampling_interval, interface, metric_ps, metric))
return line
class DiskMetricTask(RateBasedMetrics):
"""Built-in metric task to measure disk utilization metrics:
- IOps (total, in and out)
- Bps (total, in and out)
- Usage (used space in percent)
"""
def _next_line_builtin(self, args):
valid_metrics = ['iops', 'ips', 'ops', 'bps', 'bps-in', 'bps-out',
'used']
metric = 'iops'
if len(args) > 1:
metric = args[1].lower()
if metric not in valid_metrics:
metric = 'iops'
if metric == 'used':
path = '/' if len(args) < 3 else args[2].replace("'", "")
try:
usage = psutil.disk_usage(path)
total = usage.percent
except OSError:
logging.error("disk usage: path \"%s\" not found" % path)
return None
return ("Disk usage for %s|Disk=%.2f%%" % (path, total))
else:
counters = psutil.disk_io_counters()
if metric == 'iops':
total = self._calculate_total2(counters.write_count,
'write_count',
counters.read_count,
'read_count')
elif metric == 'ips':
total = self._calculate_total(counters.read_count,
'read_count')
elif metric == 'ops':
total = self._calculate_total(counters.write_count,
'write_count')
elif metric == 'bps':
total = self._calculate_total2(counters.write_bytes,
'write_bytes',
counters.read_bytes,
'read_bytes')
elif metric == 'bps-in':
total = self._calculate_total(counters.read_bytes,
'read_bytes')
elif metric == 'bps-out':
total = self._calculate_total(counters.write_bytes,
'write_bytes')
metric_ps = total / self.sampling_interval
line = ("Disk %d over %s sec|Disk=%.2f%s"
% (total, self.sampling_interval, metric_ps, metric))
return line
class AgentLoop(object):
"""Agent main loop. Setup logging, parse config file, setup metric
collection tasks and start scheduler.
"""
def __init__(self, options=None):
self.options = options
self.running = False
self.state = AgentState.IDLE
self.config = ConfigParser.ConfigParser()
self.config.readfp(codecs.open(CONFIG_FILE, 'r', 'utf-8'))
try:
agent_name = self.config.get('General', 'agent_name')
token = self.config.get('General', 'server_metrics_token')
except ConfigParser.NoSectionError:
logging.error("server metrics agent name (agent_name) and "
"token (server_metrics_token) are mandatory "
"configuration variables under the \"General\" "
"section")
sys.exit(1)
api_url = (
self.config.get('General', 'server_metrics_api_url')
if self.config.has_option('General', 'server_metrics_api_url')
else DEFAULT_SERVER_METRICS_API_URL)
proxy_url = (
self.config.get('General', 'proxy_url')
if self.config.has_option('General', 'proxy_url')
else None)
self.poll_rate = (self.config.get('General', 'poll_rate')
if self.config.has_option('General', 'poll_rate')
else DEFAULT_POLL_RATE)
self.sampling_interval = (
self.config.get('General', 'sampling_interval')
if self.config.has_option('General', 'sampling_interval')
else DEFAULT_SAMPLING_INTERVAL)
self.data_push_interval = (
self.config.get('General', 'data_push_interval')
if self.config.has_option('General', 'data_push_interval')
else DEFAULT_DATA_PUSH_INTERVAL)
self.client = ApiClient(agent_name, token, api_url, proxy_url)
self.scheduler = Scheduler()
self.queue = Queue.Queue(maxsize=100)
self.reporter = Reporting(self.queue, self.client)
self.reporter.daemon = True
def _parse_commands(self):
# Configuration options named 'command' are our tasks.
try:
for section in self.config.sections():
if self.config.has_option(section, 'command'):
cmd = self.config.get(section, 'command')
if cmd.lower().startswith('builtin'):
cmd_args = [s for s in CONFIG_CMD_ARGS_REGEX.split(cmd)
if s.strip()][1:]
if (not len(cmd_args) or
cmd_args[0].lower() not in ['cpu', 'memory',
'network', 'disk']):
logging.warning("unknown built-in command: \"%s\""
% cmd)
continue
cmd = cmd_args[0].lower()
args = (self.queue, self.client, ' '.join(cmd_args),
None, self.sampling_interval,
self.data_push_interval)
if 'cpu' == cmd:
self.scheduler.add_task(CPUMetricTask(*args))
elif 'memory' == cmd:
self.scheduler.add_task(MemoryMetricTask(*args))
elif 'network' == cmd:
self.scheduler.add_task(NetworkMetricTask(*args))
elif 'disk' == cmd:
self.scheduler.add_task(DiskMetricTask(*args))
else:
logging.warning("unknown built-in command: \"%s\""
% cmd)
else:
perf_data_opts = {}
if self.config.has_option(section, 'performance_data'):
opts = self.config.get(section, 'performance_data')
for match in PERF_DATA_OPTS_REGEX.finditer(opts):
metric = (match.group(1) if match.group(1)
else match.group(2))
perf_data_opts[metric] = match.group(3)
self.scheduler.add_task(Task(self.queue, self.client,
cmd, perf_data_opts,
self.sampling_interval,
self.data_push_interval))
except Exception:
logging.error("failed parsing commands configuration file")
log_dump()
sys.exit(1)
def run(self):
self._parse_commands()
# In test-mode we execute all plugins and prints the output
# to stdout. Then exist the main loop.
if self.options and self.options.test:
for task in self.scheduler.tasks:
print "Command: %s" % task.cmd
print " %s" % task._next_line()
return
self.reporter.start()
self.scheduler.start()
self.running = True
execution_time = time.time()
try:
while self.running:
start = time.time()
try:
# Poll server to see if we should switch state (start or
# stop sending collected data to server) or alter poll rate.
status, body = self.client.poll()
if 200 == status:
try:
j = json.loads(body)
except ValueError:
logging.error("unable to parse body as JSON: \"%s\""
% repr(body))
continue
try:
state = int(j['state'])
if state != self.state:
self.state = state
if AgentState.is_valid(state):
for task in self.scheduler.tasks:
task.set_state(state)
state_name = AgentState.get_name(state)
logging.info("switching state to %s "
"(%d)"
% (state_name, state))
else:
logging.error("received invalid state "
"from server: %d" % state)
except KeyError:
logging.error("'state' not found in JSON response")
except ValueError:
logging.error("type coercion failed: 'state' "
"(\"%s\") to int"
% str(j['state']))
try:
new_poll_rate = int(j['poll_rate'])
if new_poll_rate != self.poll_rate:
logging.info("Switching poll rate from %s to %s."
% (str(self.poll_rate), str(new_poll_rate)))
self.poll_rate = new_poll_rate
except KeyError:
logging.error("'poll_rate' not found in JSON "
"response")
except ValueError:
logging.error("type coercion failed: 'poll_rate' "
"(\"%s\") to int" % str(j['state']))
else:
logging.error("%d status code returned when "
"polling server API for state: \"%s\""
% (status, repr(body)))
except Exception:
log_dump()
finally:
execution_time += max(10, min(3600, self.poll_rate))
time.sleep(max(0, execution_time - start))
self.scheduler.stop()
except KeyboardInterrupt:
self.scheduler.stop()
def stop(self):
self.running = False
if __name__ == "__main__":
p = optparse.OptionParser(version=('%%prog %s' % __version__))
p.add_option('-c', '--config', type='string', action='store',
dest='config_file', default=CONFIG_FILE,
help="Specifies the name of the config file. Default is %s." %
CONFIG_FILE)
p.add_option('-D', '--no-daemon', action='store_false',
dest='daemon', default=True,
help=("When this option is specified, the server metrics "
"agent will not detach and does not become a daemon. "
"On windows this option is always enabled."))
p.add_option('-P', '--poll-on-start', action='store_true',
dest='poll', default=False,
help=("Poll server on startup to verify connection."))
p.add_option('--test', action='store_true',
dest='test', default=False,
help="Enable test mode. Runs all plugins once and then exists.")
opts, args = p.parse_args()
run_as_daemon = opts.daemon if running_on_linux else False
if run_as_daemon:
retcode = daemonize()
CONFIG_FILE = opts.config_file
init_logging()
if run_as_daemon:
logging.debug("server metrics agent daemonization returned: %d"
% retcode)
if running_on_linux:
try:
with open(PID_FILE, 'w') as f:
f.write(str(os.getpid()))
except IOError, e:
logging.error("unable to write pid file \"%s\": %s" % (PID_FILE,
repr(e)))
if not run_as_daemon:
print 'press Ctrl-C to stop me'
loop = AgentLoop(opts)