# Path: collectors/lib/utils.py
# PY3 = sys.version_info[0] > 2
# USER = "nobody"
# def drop_privileges(user=USER):
# def is_sockfile(path):
# def err(msg):
#     def is_numeric(value):
#     def is_numeric(value):


import sys
import socket
import time
import re
from subprocess import Popen, PIPE, CalledProcessError
from collectors.lib import utils
USER = "root"

KEYS = frozenset([
    "zk_avg_latency",
    "zk_max_latency",
    "zk_min_latency",
    "zk_packets_received",
    "zk_packets_sent",
    "zk_num_alive_connections",
    "zk_outstanding_requests",
    "zk_approximate_data_size",
    "zk_open_file_descriptor_count",
    ])

def scan_zk_instances():
    """ 
    Finding out all the running instances of zookeeper
    - Using netstat, finds out all listening java processes.	 
    - Figures out ZK instances among java processes by looking for the 
      string "org.apache.zookeeper.server.quorum.QuorumPeerMain" in cmdline.
    """

    instances = []
    try:
        netstat = Popen(["netstat", "-lnpt"], stderr=PIPE, stdout=PIPE)
        ret = netstat.wait()
        if ret:
            raise CalledProcessError(ret, "netstat -lnpt", "netstat returned code %i" % ret)
        listen_sock = netstat.stdout.read()
    except OSError:

# Path: collectors/lib/utils.py
# PY3 = sys.version_info[0] > 2
# USER = "nobody"
# def drop_privileges(user=USER):
# def is_sockfile(path):
# def err(msg):
#     def is_numeric(value):
#     def is_numeric(value):


# Path: collectors/0/zookeeper.py
import sys
import socket
import time
import re
from subprocess import Popen, PIPE, CalledProcessError
from collectors.lib import utils

USER = "root"

KEYS = frozenset([
    "zk_avg_latency",
    "zk_max_latency",
    "zk_min_latency",
    "zk_packets_received",
    "zk_packets_sent",
    "zk_num_alive_connections",
    "zk_outstanding_requests",
    "zk_approximate_data_size",
    "zk_open_file_descriptor_count",
    ])

def scan_zk_instances():
    """ 
    Finding out all the running instances of zookeeper
    - Using netstat, finds out all listening java processes.	 
    - Figures out ZK instances among java processes by looking for the 
      string "org.apache.zookeeper.server.quorum.QuorumPeerMain" in cmdline.
    """

    instances = []
    try:
        netstat = Popen(["netstat", "-lnpt"], stderr=PIPE, stdout=PIPE)
        ret = netstat.wait()
        if ret:
            raise CalledProcessError(ret, "netstat -lnpt", "netstat returned code %i" % ret)
        listen_sock = netstat.stdout.read()
    except OSError:

        utils.err("netstat is not in PATH")