~jillrouleau/charms/trusty/percona-cluster/add-nrpe-checks

« back to all changes in this revision

Viewing changes to files/check_mysql_conns.py

  • Committer: Jill Rouleau
  • Date: 2015-08-26 22:54:17 UTC
  • Revision ID: jill.rouleau@canonical.com-20150826225417-pjj90qtbfm8ll9g5
adding some logic for a nagios RO mysql user, fixed files path expected by charmhelper nrpe

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
# Copyright (C) 2015 Canonical
4
 
# All Rights Reserved
5
 
# Author: Jill Rouleau <jill.rouleau@canonical.com>
6
 
#
7
 
# Check a running MySQL instance to see if it's running hot on used connections
8
 
 
9
 
import MySQLdb
10
 
import argparse
11
 
import sys
12
 
import socket
13
 
 
14
 
hostname = socket.gethostname()
15
 
db = MySQLdb.connect(host='localhost', user='debian-sys-maint', passwd='0308NGsnQlrPaiso')
16
 
c = db.cursor()
17
 
 
18
 
 
19
 
class CheckMysql:
20
 
    c.execute("SHOW STATUS LIKE 'threads_connected';")
21
 
    result = c.fetchone()
22
 
    threads = result[1]
23
 
 
24
 
    c.execute("SHOW GLOBAL VARIABLES LIKE 'max_connections';")
25
 
    result = c.fetchone()
26
 
    maxconns = result[1]
27
 
 
28
 
percent_used = round(((float(CheckMysql.threads) / float(CheckMysql.maxconns)) * 100), 2)
29
 
 
30
 
parser = argparse.ArgumentParser()
31
 
parser.add_argument('-w', action="store", dest="warn", type=int)
32
 
parser.add_argument('-c', action="store", dest="crit", type=int)
33
 
args = parser.parse_args()
34
 
 
35
 
if percent_used < args.warn:
36
 
    print("OK - {} is using {} percent of it's {} available connections".format
37
 
          (hostname, percent_used, CheckMysql.maxconns)),
38
 
    sys.exit(0)
39
 
elif percent_used >= args.warn and percent_used < args.crit:
40
 
    print("{} is using {} out of {} available connections, {} percent".format
41
 
          (hostname, CheckMysql.threads, CheckMysql.maxconns, percent_used)),
42
 
    sys.exit(1)
43
 
elif percent_used >= args.crit:
44
 
    print("{} is using {} out of {} available connections, {} percent".format
45
 
          (hostname, CheckMysql.threads, CheckMysql.maxconns, percent_used)),
46
 
    sys.exit(2)