Welcome, guest | Sign In | My Account | Store | Cart

This is little more than a modification of my previous recipe, however, I found it useful so I thought I would post it in the hopes that someone else would as well. There is color (BOLD white really) designation for 'Query' states vs others like 'Sleep'.

Python, 49 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# Stdlib imports
import os, sys
import time
import curses

# 3rd Party imports
import MySQLdb

def getStats(scr, db=None):
    (maxY, maxX) = scr.getmaxyx()
    sql = db.cursor()
    fp = open('debug', 'w+')
    maxInfo = (maxX-75)
    while 1:
        try:
            sql.execute('SHOW PROCESSLIST;')
            scr.addstr(0, 0, time.ctime())
            scr.addstr(1, 0, 'System Load: %s' % (str(os.getloadavg())[1:][:-1]))
            scr.addstr(3, 0, '%-10s %-20s %-15s %-8s %-8s %-5s%s' % (
                'User', 'Host', 'Db', 'Command', 'Time', 'Info', ' '*(maxX-71)), curses.A_BOLD|curses.A_REVERSE)
            cnt = 5
            try:
                for row in sql.fetchall():
                    if row[4].lower().strip() == 'query':
                        scr.addstr(cnt, 0, '%-10s %-20s %-15s %-8s %-8s %-5s' % (
                            row[1], row[2].split(':')[0], row[3], row[4], row[5], row[7]),
                            curses.A_BOLD)
                    else:
                        scr.addstr(cnt, 0, '%-10s %-20s %-15s %-8s %-8s %-5s' % (
                            row[1], row[2].split(':')[0], row[3], row[4], row[5], row[7]))
                    cnt += 1
                scr.addstr(cnt, 0, ' '*(maxX-1))
            except curses.error: pass
            except IOError: pass
            scr.move(maxY-2,maxX-2)
            scr.refresh()
            time.sleep(1.5)
            scr.erase()
        except KeyboardInterrupt:
            sys.exit(-1)

if __name__ == '__main__':
    try:
        db = MySQLdb.connect(sys.argv[1], user=sys.argv[2], passwd=sys.argv[3])
    except IndexError:
        print 'Usage: %s <host> <user> <passwd>' % os.path.basename(sys.argv[0])
        sys.exit(-1)
    curses.wrapper(getStats, db)

2 comments

Mike 'Fuzzy' Partin (author) 10 years, 1 month ago  # | flag

Sorry about the typo :/

vahid chakoshy 9 years, 5 months ago  # | flag

very good thanks