Skip to content

Commit

Permalink
collectors cleanup: python2 support dropped, code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
vasiliyk committed May 16, 2024
1 parent 61135dd commit dbb7d47
Show file tree
Hide file tree
Showing 46 changed files with 1,874 additions and 1,810 deletions.
280 changes: 144 additions & 136 deletions collectors/0/couchbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,154 +21,162 @@
COLLECTION_INTERVAL = CONFIG['collection_interval']
COUCHBASE_INITFILE = CONFIG['couchbase_initfile']

KEYS = frozenset( [
'bucket_active_conns',
'cas_hits',
'cas_misses',
'cmd_get',
'cmd_set',
'curr_connections',
'curr_conns_on_port_11209',
'curr_conns_on_port_11210',
'ep_queue_size',
'ep_num_value_ejects',
'ep_num_eject_failures',
'ep_oom_errors',
'ep_tmp_oom_errors',
'get_hits',
'get_misses',
'mem_used',
'total_connections',
'total_heap_bytes',
'total_free_bytes',
'total_allocated_bytes',
'total_fragmentation_bytes',
'tcmalloc_current_thread_cache_bytes',
'tcmalloc_max_thread_cache_bytes',
'tcmalloc_unmapped_bytes',
] )
KEYS = frozenset([
'bucket_active_conns',
'cas_hits',
'cas_misses',
'cmd_get',
'cmd_set',
'curr_connections',
'curr_conns_on_port_11209',
'curr_conns_on_port_11210',
'ep_queue_size',
'ep_num_value_ejects',
'ep_num_eject_failures',
'ep_oom_errors',
'ep_tmp_oom_errors',
'get_hits',
'get_misses',
'mem_used',
'total_connections',
'total_heap_bytes',
'total_free_bytes',
'total_allocated_bytes',
'total_fragmentation_bytes',
'tcmalloc_current_thread_cache_bytes',
'tcmalloc_max_thread_cache_bytes',
'tcmalloc_unmapped_bytes',
])


def find_couchbase_pid():
"""Find out the pid of couchbase"""
if not os.path.isfile(COUCHBASE_INITFILE):
return

try:
fd = open(COUCHBASE_INITFILE)
for line in fd:
if line.startswith("exec"):
init_script = line.split()[1]
fd.close()
except IOError:
utils.err("Check permission of file (%s)" % COUCHBASE_INITFILE)
return

try:
fd = open(init_script)
for line in fd:
if line.startswith("PIDFILE"):
pid_file = line.split("=")[1].rsplit()[0]
fd.close()
except IOError:
utils.err("Check permission of file (%s)" % init_script)
return

try:
fd = open(pid_file)
pid = fd.read()
fd.close()
except IOError:
utils.err("Couchbase-server is not running, since no pid file exists")
sys.exit(13)

return pid.split()[0]
"""Find out the pid of couchbase"""
if not os.path.isfile(COUCHBASE_INITFILE):
return

try:
fd = open(COUCHBASE_INITFILE)
for line in fd:
if line.startswith("exec"):
init_script = line.split()[1]
fd.close()
except IOError:
utils.err("Check permission of file (%s)" % COUCHBASE_INITFILE)
return

try:
fd = open(init_script)
for line in fd:
if line.startswith("PIDFILE"):
pid_file = line.split("=")[1].rsplit()[0]
fd.close()
except IOError:
utils.err("Check permission of file (%s)" % init_script)
return

try:
fd = open(pid_file)
pid = fd.read()
fd.close()
except IOError:
utils.err("Couchbase-server is not running, since no pid file exists")
sys.exit(13)

return pid.split()[0]


def find_conf_file(pid):
"""Returns config file for couchbase-server."""
try:
fd = open('/proc/%s/cmdline' % pid)
except IOError as e:
utils.err("Couchbase (pid %s) went away ? %s" % (pid, e))
return
try:
config = fd.read().split("config_path")[1].split("\"")[1]
return config
finally:
fd.close()
"""Returns config file for couchbase-server."""
try:
fd = open('/proc/%s/cmdline' % pid)
except IOError as e:
utils.err("Couchbase (pid %s) went away ? %s" % (pid, e))
return
try:
config = fd.read().split("config_path")[1].split("\"")[1]
return config
finally:
fd.close()


def find_bindir_path(config_file):
"""Returns the bin directory path"""
try:
fd = open(config_file)
except IOError as e:
utils.err("Error for Config file (%s): %s" % (config_file, e))
return None
try:
for line in fd:
if line.startswith("{path_config_bindir"):
return line.split(",")[1].split("\"")[1]
finally:
fd.close()
"""Returns the bin directory path"""
try:
fd = open(config_file)
except IOError as e:
utils.err("Error for Config file (%s): %s" % (config_file, e))
return None
try:
for line in fd:
if line.startswith("{path_config_bindir"):
return line.split(",")[1].split("\"")[1]
finally:
fd.close()


def list_bucket(bin_dir):
"""Returns the list of memcached or membase buckets"""
buckets = []
if not os.path.isfile("%s/couchbase-cli" % bin_dir):
"""Returns the list of memcached or membase buckets"""
buckets = []
if not os.path.isfile("%s/couchbase-cli" % bin_dir):
return buckets
cli = ("%s/couchbase-cli" % bin_dir)
try:
buck = subprocess.check_output([cli, "bucket-list", "--cluster",
"localhost:8091"])
except subprocess.CalledProcessError:
return buckets
regex = re.compile("[\s\w]+:[\s\w]+$")
for i in buck.splitlines():
if not regex.match(i):
buckets.append(i)
return buckets
cli = ("%s/couchbase-cli" % bin_dir)
try:
buck = subprocess.check_output([cli, "bucket-list", "--cluster",
"localhost:8091"])
except subprocess.CalledProcessError:
return buckets
regex = re.compile("[\s\w]+:[\s\w]+$")
for i in buck.splitlines():
if not regex.match(i):
buckets.append(i)
return buckets


def collect_stats(bin_dir, bucket):
"""Returns statistics related to a particular bucket"""
if not os.path.isfile("%s/cbstats" % bin_dir):
return
cli = ("%s/cbstats" % bin_dir)
try:
ts = time.time()
stats = subprocess.check_output([cli, "localhost:11211", "-b", bucket,
"all"])
except subprocess.CalledProcessError:
return
for stat in stats.splitlines():
metric = stat.split(":")[0].lstrip(" ")
value = stat.split(":")[1].lstrip(" \t")
if metric in KEYS:
print("couchbase.%s %i %s bucket=%s" % (metric, ts, value, bucket))
"""Returns statistics related to a particular bucket"""
if not os.path.isfile("%s/cbstats" % bin_dir):
return
cli = ("%s/cbstats" % bin_dir)
try:
ts = time.time()
stats = subprocess.check_output([cli, "localhost:11211", "-b", bucket,
"all"])
except subprocess.CalledProcessError:
return
for stat in stats.splitlines():
metric = stat.split(":")[0].lstrip(" ")
value = stat.split(":")[1].lstrip(" \t")
if metric in KEYS:
print("couchbase.%s %i %s bucket=%s" % (metric, ts, value, bucket))


def main():
utils.drop_privileges()
pid = find_couchbase_pid()
if not pid:
utils.err("Error: Either couchbase-server is not running or file (%s)"
" doesn't exist" % COUCHBASE_INITFILE)
return 13

conf_file = find_conf_file(pid)
if not conf_file:
utils.err("Error: Can't find config file (%s)" % conf_file)
return 13

bin_dir = find_bindir_path(conf_file)
if not bin_dir:
utils.err("Error: Can't find bindir path in config file")
return 13

while True:
# Listing bucket everytime so as to start collecting datapoints
# of any new bucket.
buckets = list_bucket(bin_dir)
for b in buckets:
collect_stats(bin_dir, b)
time.sleep(COLLECTION_INTERVAL)
utils.drop_privileges()
pid = find_couchbase_pid()
if not pid:
utils.err("Error: Either couchbase-server is not running or file (%s)"
" doesn't exist" % COUCHBASE_INITFILE)
return 13 # ask tcollector to not respawn us

conf_file = find_conf_file(pid)
if not conf_file:
utils.err("Error: Can't find config file (%s)" % conf_file)
return 13

bin_dir = find_bindir_path(conf_file)
if not bin_dir:
utils.err("Error: Can't find bindir path in config file")
return 13

while True:
# Listing bucket everytime so as to start collecting datapoints
# of any new bucket.
buckets = list_bucket(bin_dir)
for b in buckets:
collect_stats(bin_dir, b)
sys.stdout.flush()
time.sleep(COLLECTION_INTERVAL)


if __name__ == "__main__":
sys.exit(main())
sys.exit(main())
Loading

0 comments on commit dbb7d47

Please sign in to comment.