Python Forum
hatop issue - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hatop issue (/thread-22824.html)

Pages: 1 2 3


hatop issue - deepakkr3110 - Nov-28-2019

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


RE: hatop issue - Gribouillis - Nov-28-2019

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')



RE: hatop issue - deepakkr3110 - Nov-28-2019

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'


RE: hatop issue - Gribouillis - Nov-28-2019

Remove the split('\n') and see what is the value of the sockets variable.


RE: hatop issue - deepakkr3110 - Nov-28-2019

nothing print after removing
.split('\n')



RE: hatop issue - Gribouillis - Nov-28-2019

add print(repr(sockets))


RE: hatop issue - deepakkr3110 - Nov-28-2019

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


RE: hatop issue - Gribouillis - Nov-28-2019

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.


RE: hatop issue - deepakkr3110 - Nov-28-2019

this is the code on github https://github.com/JamesBarwell/haproxy-status.py have a look


RE: hatop issue - Gribouillis - Nov-28-2019

Sorry, I don't see the use of configparser in your code. You could perhaps explain what you want to do with more details.