~arosales/charms/precise/nagios/better-icon

« back to all changes in this revision

Viewing changes to tests/100-deploy

  • Committer: amulet at dummy-user
  • Date: 2014-03-29 23:29:09 UTC
  • mfrom: (10.1.2 nagios)
  • Revision ID: amulet@dummy-user.tld-20140329232909-zgxcqbetoo1eyhz8
[lazypower] Amulet Tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
#TODO - Discover what service mymonitors was designed for - as the charm store has nothing listed for this interface.
 
4
import amulet
 
5
import requests
 
6
 
 
7
###
 
8
# Quick Config
 
9
###
 
10
seconds = 900
 
11
 
 
12
 
 
13
d = amulet.Deployment()
 
14
 
 
15
d.add('haproxy')
 
16
d.add('nagios')
 
17
d.add('mysql')
 
18
d.add('nrpe')
 
19
 
 
20
#TODO - configure nagios with SSL options in branch
 
21
# lp:~lazypower/charms/precise/nagios/ssl-everywhere
 
22
# pending lp #1293793
 
23
d.configure('nagios', {'extraconfig': '#amulet'})
 
24
d.configure('haproxy', {})
 
25
d.configure('mysql', {})
 
26
 
 
27
d.relate('nagios:website', 'haproxy:reverseproxy')
 
28
d.relate('nagios:monitors', 'mysql:monitors')
 
29
d.relate('nrpe:general-info', 'haproxy:juju-info')
 
30
d.relate('nrpe:monitors', 'nagios:monitors')
 
31
 
 
32
d.expose('nagios')
 
33
d.expose('haproxy')
 
34
 
 
35
try:
 
36
    d.setup(timeout=seconds)
 
37
    #d.sentry.wait()
 
38
except amulet.helpers.TimeoutError:
 
39
    amulet.raise_status(amulet.SKIP, msg="Environment wasn't stood up in time")
 
40
except:
 
41
    raise
 
42
 
 
43
 
 
44
##
 
45
# Set relationship aliases
 
46
##
 
47
mysql_unit = d.sentry.unit['mysql/0']
 
48
nagios_unit = d.sentry.unit['nagios/0']
 
49
haproxy_unit = d.sentry.unit['haproxy/0']
 
50
 
 
51
# Fetch nagios private address through the relation data
 
52
sql_paddr = d.sentry.unit['nagios/0'].relation('monitors', 'mysql:monitors')['private-address']
 
53
 
 
54
 
 
55
# Mysql has a nice built in monitoring relationship with nagios
 
56
# Validate that we get the proper tables setup
 
57
def test_mysql_monitoring_connection():
 
58
    validation_sql = "select * from information_schema.user_privileges where grantee like \\\"'monitors'%\\\";"
 
59
    validation_command = "mysql -u root -p`cat /var/lib/mysql/mysql.passwd` -e \"%s\"" % validation_sql
 
60
    output, code = mysql_unit.run(validation_command)
 
61
    #We know that we need a GRANT USAGE permission for this to work properly
 
62
    if output.find("USAGE") == -1:
 
63
        amulet.raise_status(amulet.FAIL, msg="Missing GRANT on MySQL unit")
 
64
 
 
65
 
 
66
# We have an issue here. We cannot assign a sentry unit to a
 
67
# subordinate. This poses an interesting problem.
 
68
# validate that haproxy has an nrpe config, and *assume* its ok for now - needs
 
69
# some love otherwise.
 
70
def test_nrpe_monitor():
 
71
    #On join the NRPE unit generates an allowed hosts config
 
72
    ahosts = haproxy_unit.file_stat('/etc/nagios/nrpe.d/allowed.hosts.cfg')
 
73
    if not ahosts:
 
74
        amulet.raise_status(amulet.FAIL, msg="Missing nrpe config")
 
75
    running, code = haproxy_unit.run('service nagios-nrpe-server status')
 
76
    if running.find('running') == -1:
 
77
        amulet.raise_status(amulet.FAIL, msg="Failed to find running nrpe daemon")
 
78
 
 
79
 
 
80
#Validate that the web interface has htpasswd authentication
 
81
def test_web_interface_is_protected():
 
82
    r = requests.get("http://%s/nagios3/" % nagios_unit.info['public-address'])
 
83
    if r.status_code != 401:
 
84
        amulet.raise_status(amulet.FAIL, msg="Web Interface open to the world")
 
85
    #validate that our configured admin is valid
 
86
    nagpwd = nagios_unit.file_contents('/var/lib/juju/nagios.passwd').strip()
 
87
    r = requests.get("http://%s/nagios3/" % nagios_unit.info['public-address'],
 
88
                     auth=('nagiosadmin', nagpwd))
 
89
    if r.status_code != 200:
 
90
        amulet.raise_status(amulet.FAIL, msg="Web Admin login failed")
 
91
 
 
92
 
 
93
def text_extra_config_is_written():
 
94
    extracfg = nagios_unit.file_stat('/etc/nagios3/conf.d/extra.cfg')
 
95
    if not extracfg:
 
96
        amulet.raise_status(amulet.FAIL, msg="Extra Config missing")
 
97
    extracont = nagios_unit.file_contents('/etc/nagios3/conf.d/extra.cfg')
 
98
    if extracont.find('#amulet') == -1:
 
99
        amulet.raise_status(amulet.FAIL, msg="Missing extra config contents")
 
100
 
 
101
#TODO validate SQLite database entries for units
 
102
 
 
103
 
 
104
test_mysql_monitoring_connection()
 
105
test_nrpe_monitor()
 
106
test_web_interface_is_protected()