Merge lp:~jacekn/charms/precise/mysql/n-e-m-relation-v2 into lp:charms/mysql

Proposed by Jacek Nykis
Status: Merged
Merged at revision: 125
Proposed branch: lp:~jacekn/charms/precise/mysql/n-e-m-relation-v2
Merge into: lp:charms/mysql
Prerequisite: lp:~jacekn/charms/precise/mysql/n-e-m-relation
Diff against target: 101 lines (+52/-3)
3 files modified
hooks/config-changed (+1/-1)
hooks/nrpe_relations.py (+50/-1)
revision (+1/-1)
To merge this branch: bzr merge lp:~jacekn/charms/precise/mysql/n-e-m-relation-v2
Reviewer Review Type Date Requested Status
Charles Butler (community) Approve
Review via email: mp+218969@code.launchpad.net

Description of the change

Added another nrpe check for better monitoring

To post a comment you must log in.
Revision history for this message
Benjamin Saller (bcsaller) wrote :

+1 Thanks.

I included a minor suggestion about a way to slightly cleanup the add_nagios_user call.

Revision history for this message
Benjamin Saller (bcsaller) wrote :

This is a +1 from me. A ~charmer will be by shortly to complete the review and promulgate this charm if there’s no other issues. Thanks for your submission thus far!

I included a minor suggestion about a way to slightly cleanup the add_nagios_user call.

Revision history for this message
Charles Butler (lazypower) wrote :

I just had a minor knitpick on the use of os.path.join without using os.path.sep to set the root - as this will break on non posix systems.

Otherwise LGTM.

review: Approve
Revision history for this message
Charles Butler (lazypower) wrote :

I've gone ahead and committed this as is, considering there were only minor knitpicks.

Thanks for the submission Jacek!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hooks/config-changed'
2--- hooks/config-changed 2014-05-09 11:20:25 +0000
3+++ hooks/config-changed 2014-05-09 11:20:25 +0000
4@@ -380,4 +380,4 @@
5 check_call(['service', 'mysql', 'restart'])
6 if relations_of_type('nrpe-external-master'):
7 import nrpe_relations
8- nrpe_relations.update_nrpe_checks()
9+ nrpe_relations.nrpe_relation()
10
11=== modified file 'hooks/nrpe_relations.py'
12--- hooks/nrpe_relations.py 2014-05-09 11:20:25 +0000
13+++ hooks/nrpe_relations.py 2014-05-09 11:20:25 +0000
14@@ -1,17 +1,60 @@
15 #!/usr/bin/env python
16
17+import os
18 import sys
19+import uuid
20+import MySQLdb
21 from charmhelpers.core.hookenv import (
22 log,
23 relations_of_type,
24 Hooks, UnregisteredHookError
25 )
26 from charmhelpers.contrib.charmsupport.nrpe import NRPE
27+from common import get_db_cursor
28
29
30 hooks = Hooks()
31
32
33+def nagios_password():
34+ PASSFILE = os.path.join('/var', 'lib', 'mysql', 'nagios.passwd')
35+ if not os.path.isfile(PASSFILE):
36+ password = str(uuid.uuid4())
37+ with open(PASSFILE, 'w') as f:
38+ f.write(password)
39+ os.chmod(PASSFILE, 0600)
40+ else:
41+ with open(PASSFILE, 'r') as rpw:
42+ password = rpw.read()
43+ return password
44+
45+
46+def add_nagios_user():
47+ c = get_db_cursor()
48+ if c.execute("SELECT User from mysql.user where User='nagios'"):
49+ log('User nagios already exists, skipping')
50+ else:
51+ log('Creating "nagios" database user')
52+ password = nagios_password()
53+ c.execute("CREATE USER 'nagios'@'localhost' IDENTIFIED BY '{}'"
54+ "".format(password))
55+
56+ try:
57+ c.execute("SHOW GRANTS FOR 'nagios'@'localhost'")
58+ grants = [i[0] for i in c.fetchall()]
59+ except MySQLdb.OperationalError:
60+ grants = []
61+
62+ for grant in grants:
63+ if "GRANT PROCESS ON *.*" in grant:
64+ log('User already has permissions, skipping')
65+ c.close()
66+ return
67+ log('Granting "PROCESS" privilege to nagios user')
68+ c.execute("GRANT PROCESS ON *.* TO 'nagios'@'localhost'")
69+ c.close()
70+
71+
72 def update_nrpe_checks():
73 log('Refreshing nrpe checks')
74 # Find out if nrpe set nagios_hostname
75@@ -26,12 +69,18 @@
76 description='Check MySQL process',
77 check_cmd='check_procs -c 1:1 -C mysqld'
78 )
79+ nrpe.add_check(
80+ shortname='mysql',
81+ description='Check MySQL connectivity',
82+ check_cmd='check_mysql -u nagios -p {}'.format(nagios_password())
83+ )
84 nrpe.write()
85
86
87 @hooks.hook('nrpe-external-master-relation-changed')
88 @hooks.hook('nrpe-external-master-relation-joined')
89-def add_nrpe_relation():
90+def nrpe_relation():
91+ add_nagios_user()
92 update_nrpe_checks()
93
94
95
96=== modified file 'revision'
97--- revision 2014-05-09 11:20:25 +0000
98+++ revision 2014-05-09 11:20:25 +0000
99@@ -1,1 +1,1 @@
100-321
101+325

Subscribers

People subscribed via source and target branches

to all changes: