Add generic script to check mountpoints, for collectd

This commit is contained in:
Lance Edgar 2022-08-21 22:57:30 -05:00
parent 987731885b
commit a2dca4ea65
2 changed files with 100 additions and 0 deletions

View file

@ -0,0 +1,37 @@
# -*- coding: utf-8; -*-
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2022 Lance Edgar
#
# This file is part of Rattail.
#
# Rattail is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# Rattail. If not, see <http://www.gnu.org/licenses/>.
#
################################################################################
"""
Fabric library for collectd
"""
from rattail_fabric2 import make_deploy
deploy = make_deploy(__file__)
def deploy_mountpoint_check_script(c, dest, **kwargs):
"""
Deploy generic mountpoint check script to specified destination.
"""
deploy(c, 'collectd/check-mountpoints.py', dest, **kwargs)

View file

@ -0,0 +1,63 @@
# -*- coding: utf-8; -*-
import argparse
import os
import socket
import subprocess
import sys
def check_mounts(mountpoints):
for path in mountpoints:
sys.stdout.write("{} is {}mounted\n".format(
path, '' if is_mounted(path) else 'NOT '))
def check_mounts_collectd(mountpoints):
hostname = os.environ.get('COLLECTD_HOSTNAME')
if not hostname:
hostname = socket.getfqdn()
plugin = 'mountpoints'
interval = os.environ.get('COLLECTD_INTERVAL')
if interval:
interval = ' interval={}'.format(interval)
else:
interval = ''
for path in mountpoints:
name = path.strip('/').replace('/', '-')
plugin_instance = '{}-{}'.format(plugin, name)
data_type = 'gauge-state'
value = 1 if is_mounted(path) else 0
value = 'N:{}'.format(value)
msg = 'PUTVAL {}/{}/{}{} {}\n'.format(
hostname,
plugin_instance,
data_type,
interval,
value)
sys.stdout.write(msg)
def is_mounted(path):
cmd = "mount | grep '{}' || true".format(path)
output = subprocess.check_output(cmd, shell=True)
return bool(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--collectd', action='store_true')
parser.add_argument('mountpoints', nargs='+', metavar='MOUNTPOINT')
args = parser.parse_args()
if args.collectd:
check_mounts_collectd(args.mountpoints)
else:
check_mounts(args.mountpoints)