1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+ #
4+ import sys
5+ import optparse
6+ import socket
7+ import simplejson
8+ import re
9+
10+ import protobix
11+
12+ import getopt
13+ from telnetlib import Telnet
14+
15+ # ZooKeeper Commands: The Four Letter Words
16+ # Referer: http://zookeeper.apache.org/doc/r3.4.6/zookeeperAdmin.html#sc_zkCommands
17+
18+ CommandKey = {
19+ 'conf' :['clientPort' ,'dataDir' ,'dataLogDir' ,'tickTime' ,'maxClientCnxns' ,'minSessionTimeout' ,'maxSessionTimeout' ,'serverId' ,'initLimit' ,'syncLimit' ,'electionAlg' ,'electionPort' ,'quorumPort' ,'peerType' ],
20+ 'ruok' :['state' ],
21+ 'mntr' :['zk_version' ,'zk_avg_latency' ,'zk_max_latency' ,'zk_min_latency' ,'zk_packets_received' ,'zk_packets_sent' ,'zk_num_alive_connections' ,'zk_outstanding_requests' ,'zk_server_state' ,'zk_znode_count' ,'zk_watch_count' ,'zk_ephemerals_count' ,'zk_approximate_data_size' ,'zk_open_file_descriptor_count' ,'zk_max_file_descriptor_count' ,'zk_followers' ,'zk_synced_followers' ,'zk_pending_syncs' ]
22+ }
23+
24+ class ZooKeeperServer (protobix .SampleProbe ):
25+ __version__ = "0.0.9"
26+
27+ def _parse_args (self ):
28+ ''' Parse the script arguments
29+ '''
30+ parser = super ( ZooKeeperServer , self )._parse_args ()
31+
32+ general_options = optparse .OptionGroup (parser , "ZooKeeper "
33+ "configuration options" )
34+ general_options .add_option ("-H" , "--host" , metavar = "HOST" , default = "127.0.0.1" ,
35+ help = "Server FQDN" )
36+ general_options .add_option ("-C" , "--zkCommand" , metavar = "COMMAND" , default = "mntr" ,
37+ help = "ZooKeeper command" )
38+ general_options .add_option ("-P" , "--port" , default = 2181 ,
39+ help = "ZooKeeper port"
40+ "Default is 2181" )
41+ parser .add_option_group (general_options )
42+
43+ (options , args ) = parser .parse_args ()
44+ return (options , args )
45+ def _init_probe (self ):
46+ if self .options .host == 'localhost' or self .options .host == '127.0.0.1' :
47+ self .hostname = socket .getfqdn ()
48+ else :
49+ self .hostname = self .options .host
50+ def _get_metrics (self ):
51+ data = {}
52+ s = socket .socket ( socket .AF_INET , socket .SOCK_STREAM )
53+ s .connect ( ( self .options .host , self .options .port ) )
54+ s .send ( self .options .zkCommand )
55+ rawdata = ''
56+ output = s .recv (1024 )
57+ while output :
58+ rawdata += output
59+ output = s .recv (1024 )
60+ s .close
61+ if (rawdata ):
62+ items = {}
63+ if self .options .zkCommand == 'mntr' :
64+ for line in rawdata .splitlines () :
65+ parts = line .split ('\t ' )
66+ index = parts [0 ]
67+ items [index ] = parts [1 ]
68+ elif self .options .zkCommand == 'conf' :
69+ for line in rawdata .splitlines () :
70+ parts = line .split ('=' )
71+ index = parts [0 ]
72+ items [index ] = parts [1 ]
73+ elif self .options .zkCommand == 'ruok' :
74+ if rawdata == 'imok' :
75+ items ['state' ] = 1
76+ else :
77+ items ['state' ] = 0
78+ for item in items :
79+ data [("zookeeper.%s" % item )] = items [item ]
80+ data ['zookeeper.zbx_version' ] = self .__version__
81+ return { self .hostname : data }
82+
83+ if __name__ == '__main__' :
84+ ret = ZooKeeperServer ().run ()
85+ print ret
86+ sys .exit (ret )
0 commit comments