|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2018, Rackspace US, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +import shlex |
| 19 | +import subprocess |
| 20 | + |
| 21 | +from maas_common import metric |
| 22 | +from maas_common import metric_bool |
| 23 | +from maas_common import print_output |
| 24 | +from maas_common import status_err |
| 25 | +from maas_common import status_ok |
| 26 | + |
| 27 | + |
| 28 | +def nfs_export_check(): |
| 29 | + nfs_metrics = dict() |
| 30 | + |
| 31 | + output = subprocess.check_output( |
| 32 | + shlex.split( |
| 33 | + "awk -F':' '/nfs/ {print $1}' /proc/mounts" |
| 34 | + ) |
| 35 | + ) |
| 36 | + nfs_mounts = output.splitlines() |
| 37 | + for mount in nfs_mounts: |
| 38 | + mount_metrics = nfs_metrics[mount] = {'exports': 0, 'online': False} |
| 39 | + try: |
| 40 | + exports = subprocess.check_output( |
| 41 | + shlex.split( |
| 42 | + "showmount --no-headers --exports {}".format(mount) |
| 43 | + ) |
| 44 | + ) |
| 45 | + except subprocess.CalledProcessError: |
| 46 | + mount_metrics['exports'] = 0 |
| 47 | + else: |
| 48 | + mount_metrics['exports'] += len(exports.splitlines()) |
| 49 | + |
| 50 | + if mount_metrics['exports'] > 0: |
| 51 | + mount_metrics['online'] = True |
| 52 | + |
| 53 | + return nfs_metrics |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + parser = argparse.ArgumentParser(description='Disk utilisation checks') |
| 58 | + parser.add_argument('--telegraf-output', |
| 59 | + action='store_true', |
| 60 | + default=False, |
| 61 | + help='Set the output format to telegraf') |
| 62 | + args = parser.parse_args() |
| 63 | + with print_output(print_telegraf=args.telegraf_output): |
| 64 | + nfs_system_check = nfs_export_check() |
| 65 | + |
| 66 | + # If there are no returns for the nfs check or the status is ALL |
| 67 | + # online the check is marked as "OK". |
| 68 | + status = all([i['online'] for i in nfs_system_check.values()]) |
| 69 | + if status: |
| 70 | + status_ok(m_name='nfs_check') |
| 71 | + else: |
| 72 | + status_err('The state of NFS connections is degraded', |
| 73 | + m_name='nfs_check') |
| 74 | + |
| 75 | + # Generate a general purpose metric fot the operational state of NFS. |
| 76 | + metric_bool('nfs_all_online', status, m_name='nfs_check') |
| 77 | + |
| 78 | + for key, value in nfs_system_check.items(): |
| 79 | + # Generate a metric for the number of exports seen from a server. |
| 80 | + metric('nfs_{}_exports'.format(key), |
| 81 | + 'uint32', |
| 82 | + value['exports'], |
| 83 | + 'exports', |
| 84 | + m_name='nfs_check') |
| 85 | + |
| 86 | + # Generate a metric for the given online or offline state. |
| 87 | + metric_bool('nfs_{}_online'.format(key), |
| 88 | + value['online'], |
| 89 | + m_name='nfs_check') |
0 commit comments