-
Notifications
You must be signed in to change notification settings - Fork 29
/
xenserver_check_bonds.py
executable file
·129 lines (99 loc) · 3.52 KB
/
xenserver_check_bonds.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/python
# Copyright 2015, Schuberg Philis BV
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import sys
import socket
import time
import re
import os
import XenAPI
# check xen bond status
DEBUG = False
def log(s):
if DEBUG:
print s
def get_bonds(session, host):
bonds = {}
slaves = {}
for (b, brec) in session.xenapi.PIF.get_all_records().items():
# only local interfaces
if brec['host'] != host:
continue
# save masters and slaves
if brec['bond_master_of']:
bonds[b] = brec
if brec['bond_slave_of']:
slaves[b] = brec
for (m, mrec) in session.xenapi.PIF_metrics.get_all_records().items():
for srec in slaves.values():
if srec['metrics'] != m:
continue
srec['carrier'] = mrec['carrier']
for (n, nrec) in session.xenapi.network.get_all_records().items():
for brec in bonds.values():
if brec['network'] != n:
continue
brec['name_label'] = nrec['name_label']
return bonds, slaves
def get_bond_status(session, host):
status = {}
for (b, brec) in session.xenapi.Bond.get_all_records().items():
status[b] = brec
return status
def main():
# First acquire a valid session by logging in:
session = XenAPI.xapi_local()
session.xenapi.login_with_password("root", "")
hostname = socket.gethostname()
host = (session.xenapi.host.get_by_name_label(hostname))[0]
# warning when host not found...
if not host:
print "failed to detect XAPI host for '%s'" % hostname
sys.exit(1)
bonds, slaves = get_bonds(session, host)
bond_status = get_bond_status(session, host)
clist = []
olist = []
# iterate over the bonds
for b in bonds:
net = bonds[b]['name_label']
ref = bonds[b]['bond_master_of'][0]
status = bond_status[ref]
# On XenServer 6.0 we manually build links_up by checking carrier
if 'links_up' not in status:
status['links_up'] = 0
for slave in status['slaves']:
if slaves[slave]['carrier']:
status['links_up'] += 1
if len(status['slaves']) != int(status['links_up']):
clist.append("%s has only %s links up (%s slaves)"
% (net, status['links_up'], len(status['slaves'])))
else:
olist.append("%s %s links up" % (net, status['links_up']))
if len(clist):
print "CRITICAL:", ", ".join(clist)
return 2
elif len(olist):
print "OK:", ", ".join(olist)
return 0
else:
print "OK: no bonds found"
return 0
if __name__ == "__main__":
sys.exit(main())