def main(args): utils.drop_privileges() if json is None: utils.err("This collector requires the `json' Python module.") return 13 # Ask tcollector not to respawn us hbase_service = HBaseMaster() hbase_service.emit()
def main(args): utils.drop_privileges() if json is None: utils.err("This collector requires the `json' Python module.") return 13 # Ask tcollector not to respawn us name_node_service = HadoopNameNode() name_node_service.emit()
def find_sock_file(conf_file): """Returns the unix socket file of haproxy.""" try: fd = open(conf_file) except IOError, e: utils.err("Error: %s. Config file path is relative: %s" % (e, conf_file)) return None
def find_conf_file(pid): """Returns the conf file of haproxy.""" try: output = subprocess.check_output( ["ps", "--no-headers", "-o", "cmd", pid]) except subprocess.CalledProcessError, e: utils.err("HAProxy (pid %s) went away? %s" % (pid, e)) return None
def connect_socket(tcp_version, port): sock = None if tcp_version == "tcp6": sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) ipaddr = '::1' else: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ipaddr = '127.0.0.1' try: sock.connect((ipaddr, port)) except Exception, err: utils.err(err)
def main(): pid = haproxy_pid() if not pid: utils.err("Error: HAProxy is not running") return 13 # Ask tcollector to not respawn us. conf_file = find_conf_file(pid) if not conf_file: return 13 sock_file = find_sock_file(conf_file) if sock_file is None: utils.err("Error: HAProxy is not listening on any unix domain socket") return 13 collect_stats(sock_file)
def emit_region_metric(self, context, current_time, full_metric_name, value, step): match = REGION_METRIC_PATTERN.match(full_metric_name) if not match: utils.err("Error splitting %s" % full_metric_name) return namespace = match.group(1) table = match.group(2) region = match.group(3) metric_name = match.group(4) tag_dict = {"namespace": namespace, "table": table, "region": region} if any(not v for k, v in tag_dict.iteritems()): utils.err("Error splitting %s" % full_metric_name) else: self.emit_metric(context, current_time, metric_name, value, step, tag_dict)
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") return instances except CalledProcessError, err: utils.err("Error: %s" % err)
def collect_stats(sock_file): """Collects stats from haproxy unix domain socket""" sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) try: sock.settimeout(COLLECTION_INTERVAL) sock.connect(sock_file) sock.send("show stat\n") statlines = sock.recv(10240).split('\n') finally: sock.close() ts = time.time() # eat up any empty lines that may be present statlines = [line for line in statlines if line != ""] # headers are given first, with or without the prompt present headers = None if statlines[0].startswith("> # "): headers = statlines[0][4:].split(',') elif statlines[0].startswith("# "): headers = statlines[0][2:].split(',') else: utils.err("No headers found in HAProxy output: %s" % (statlines[0], )) return reader = csv.DictReader(statlines[1:], fieldnames=headers) # each line is a dict, due to the use of DictReader for line in reader: if "svname" not in line: continue # skip output from non-expected lines if line["svname"] in ["FRONTEND", "BACKEND"]: for key in METRICS_TO_REPORT[line["svname"]]: print_metric(line, key, ts) else: # svname apparently points to individual server for key in METRICS_TO_REPORT["servers"]: print_metric(line, key, ts) # make sure that we get our output quickly sys.stdout.flush()
ip = m.group(1) port = int(m.group(2)) pid = int(line.split()[6].split("/")[0]) try: fd = open("/proc/%d/cmdline" % pid) cmdline = fd.readline() if "org.apache.zookeeper.server.quorum.QuorumPeerMain" in cmdline: data = None try: sock = connect_socket(tcp_version, port) sock.settimeout(0.5) sock.send("ruok\n") data = sock.recv(1024) except Exception, err: utils.err(err) finally: if sock: sock.close() if data == "imok": instances.append([ip, port, tcp_version]) data = "" except Exception, err: utils.err(err) finally: fd.close() return instances def print_stat(metric, ts, value, tags=""): if value is not None: