Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hatop issue
#1
i have a code for hatop in python
#!/usr/bin/python

import socket
import csv
import sys

sys.stdout = open('output.txt','wt')

RECV_SIZE = 1024

PRINT_COLS = {
    'pxname': 0,
    'svname': 1,
    'status': 17,
    'weight': 18,
    'chkdown': 22,
    'lastchg': 23,
    'rate': 33,
    'rate_max': 35,
    'check_status': 36,
    'rtime': 60,
}

DEBUG = False

def execute(cmd, socketPath):
    try:
        client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    except:
        print "Could not initialise socket client"
        sys.exit(1)
    try:
        client.connect(socketPath)
    except:
        print "Could not connect to socket: %s" % socket
        sys.exit(1)
    client.sendall('%s\n' % cmd)
    result = ''
    buf = ''
    buf = client.recv(RECV_SIZE)
    while buf:
        result += buf
        buf = client.recv(RECV_SIZE)
    client.close()
    return result

def get_stats(socket):
    if DEBUG:
        testdata_file = open('./testdata', 'r')
        raw_stats = testdata_file.read()
    else:
        raw_stats = execute('show stat', socket)
    return csv.reader(raw_stats.strip().split('\n'), delimiter=',')

def split_host(line):
    return line.split(',')

def is_host(line):
    svname = line[1]
    is_host = svname != "FRONTEND" and svname != "BACKEND"
    return is_host

def get_hosts(stats):
    return filter(is_host, stats)

def print_table(socket_name, table):
    table = strip_table(table)

    table[0].insert(0, 'socket')
    for row in table[1:]:
        row.insert(0, socket_name)

    table = map(insert_table_spaces, table)

    col_widths = [max(len(x) for x in col) for col in zip(*table)]
    for row in table:
        print_row = ''
        for i in range(0, len(row)):
            print_row += row[i].ljust(col_widths[i])
        print print_row

def strip_table(table):
    kept = []
    for row in table:
        kept_row = []
        for i, item in enumerate(row):
            if i in PRINT_COLS.values():
                kept_row.append(item)
        kept.append(kept_row)
    return kept

def insert_table_spaces(row):
    rtn = []
    for i, item in enumerate(row):
        if i > 0:
            rtn.append('|| ' + item)
        else:
            rtn.append(item)
    return rtn

def main(sockets):
    for socket in sockets:
        stats = get_stats(socket)
        hosts = get_hosts(stats)
        print_table(socket, hosts)

if __name__ == "__main__":
    main(sys.argv[1:])
but the issue is i have multiple sock files and i want to keep multiple sock file in an other file or in same program so that it will read it in command line. right now its working as

python hatop.py /var/run/haproxy-main-hello.sock /var/run/haproxy-main-hello1.sock

but i want python hatop.py so it will print the output from the sock files
Reply
#2
If I understand the question well, you want to read the paths to the sockets in an external file. You can use a configuration file for this and the configparser module

Output:
# configuration-file.ini [spam] sockets: /var/run/haproxy-main-hello.sock /var/run/haproxy-main-hello1.sock
Your program
import configparser
config = configparser.ConfigParser()
config.read('configuration-file.ini')
sockets = config.get('spam', 'sockets').split('\n')
Reply
#3
getting error

Traceback (most recent call last):
File "hatop.py", line 9, in <module>
sockets = config.read('spam', 'sockets').split('\n')
AttributeError: 'list' object has no attribute 'split'
Reply
#4
Remove the split('\n') and see what is the value of the sockets variable.
Reply
#5
nothing print after removing
.split('\n')
Reply
#6
add print(repr(sockets))
Reply
#7
python hatop.py
[]
this the output

python hatop.py /var/run/haproxy-main-hello.sock give me output but python hatop.py not

this is the code on github https://github.com/JamesBarwell/haproxy-status.py
Reply
#8
Can't you read the configuration file with configparser? This built-in module is very easy to use. Have a look at this tutorial. Try to run the examples.
Reply
#9
this is the code on github https://github.com/JamesBarwell/haproxy-status.py have a look
Reply
#10
Sorry, I don't see the use of configparser in your code. You could perhaps explain what you want to do with more details.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need code for hatop in python deepakkr3110 2 2,042 Nov-15-2019, 09:06 AM
Last Post: deepakkr3110

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020