Skip to content

Commit

Permalink
fs-ro: Improve code and add one unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslf committed Mar 30, 2024
1 parent 0f1cbed commit 6d0c396
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Monitoring Plugins:
* feed: Make use of `--insecure` `--no-proxy` `--timeout`
* fs-ro: Add `/dev/loop` to default ignore list
* fs-ro: Make output better readable ([PR #729](https://github.com/Linuxfabrik/monitoring-plugins/issues/729), thanks to [Konrad Bucheli](https://github.com/kbucheli))
* fs-ro: Show mount point info on first line when there is only one hit ([PR #730](https://github.com/Linuxfabrik/monitoring-plugins/issues/730), thanks to [Konrad Bucheli](https://github.com/kbucheli))
* githubstatus: Add new parameters `--insecure` `--no-proxy` `--timeout`
* gitlab-health: Add new parameters `--insecure` `--no-proxy`
* gitlab-liveness: Add new parameters `--insecure` `--no-proxy`
Expand Down
18 changes: 10 additions & 8 deletions check-plugins/fs-ro/fs-ro
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from lib.globals import (STATE_OK, STATE_UNKNOWN, # pylint: disable=C0413
STATE_WARN)

__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2023121201'
__version__ = '2024033001'

DESCRIPTION = """This plugin checks for read-only mount points, such as `/` mounted read-only due
to file system errors, mounted CD-ROMs or ISO files, etc. It always ignores ramfs
Expand Down Expand Up @@ -86,11 +86,11 @@ def main():
mounts = lib.base.coe(lib.disk.read_file('/proc/mounts'))
else:
# do not call the command, put in test data
stdout, stderr, retc = lib.test.test(args.TEST)
stdout, _, _ = lib.test.test(args.TEST)
mounts = stdout

cnt = 0
ros = [] # read-only mount points
ros = [] # read-only mount points
for mount in mounts.splitlines():
mnt = mount.split()
if len(mnt) != 6:
Expand All @@ -104,7 +104,6 @@ def main():

if mount_type in ['ramfs', 'squashfs']:
continue

mount_options = mnt[3]
if any(mount_point.startswith(item) for item in args.IGNORE):
continue
Expand All @@ -115,13 +114,16 @@ def main():

# build the message
if len(ros) > 0:
msg = '{} read-only mount {} found: '.format(
len(ros),
lib.txt.pluralize('point', len(ros)),
)
if len(ros) == 1:
msg = 'read-only mount point found: ' + ros[0]
# show mount point info on first line when there is only one hit
msg += ros[0]
else:
msg = '{} read-only mount points found:\n'.format(len(ros))
for item in ros:
msg += '* {}\n'.format(item)

msg += '\n* {}'.format(item)
state = STATE_WARN
else:
msg = 'Everything is ok. {} mount {} checked.'.format(cnt, lib.txt.pluralize('point', cnt))
Expand Down
6 changes: 6 additions & 0 deletions check-plugins/fs-ro/unit-test/run
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class TestCheck(unittest.TestCase):
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_OK)

def test_if_check_runs_EXAMPLE13(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE13,,0'))
self.assertRegex(stdout, r'3 read-only mount points found')
self.assertEqual(stderr, '')
self.assertEqual(retc, STATE_WARN)

# non-existent test file
def test_if_check_runs_EXAMPLE99(self):
stdout, stderr, retc = lib.base.coe(lib.shell.shell_exec(self.check + ' --test=stdout/EXAMPLE99,,0'))
Expand Down
29 changes: 29 additions & 0 deletions check-plugins/fs-ro/unit-test/stdout/EXAMPLE13
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
sysfs /sys sysfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
devtmpfs /dev devtmpfs rw,seclabel,nosuid,size=7959536k,nr_inodes=1989884,mode=755 0 0
securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /dev/shm tmpfs rw,seclabel,nosuid,nodev 0 0
devpts /dev/pts devpts rw,seclabel,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
cgroup2 /sys/fs/cgroup cgroup2 rw,seclabel,nosuid,nodev,noexec,relatime,nsdelegate 0 0
pstore /sys/fs/pstore pstore rw,seclabel,nosuid,nodev,noexec,relatime 0 0
efivarfs /sys/firmware/efi/efivars efivarfs rw,nosuid,nodev,noexec,relatime 0 0
none /sys/fs/bpf bpf rw,nosuid,nodev,noexec,relatime,mode=700 0 0
configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
/dev/mapper/luks-98b423af-3012-475c-bd85-456028744d69 / ext4 rw,seclabel,relatime 0 0
selinuxfs /sys/fs/selinux selinuxfs rw,relatime 0 0
systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=30,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=252 0 0
debugfs /sys/kernel/debug debugfs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
mqueue /dev/mqueue mqueue rw,seclabel,nosuid,nodev,noexec,relatime 0 0
hugetlbfs /dev/hugepages hugetlbfs rw,seclabel,relatime,pagesize=2M 0 0
tracefs /sys/kernel/tracing tracefs rw,seclabel,nosuid,nodev,noexec,relatime 0 0
fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
tmpfs /tmp tmpfs rw,seclabel,nosuid,nodev 0 0
/dev/nvme0n1p2 /boot ext4 rw,seclabel,relatime 0 0
/dev/nvme0n1p1 /boot/efi vfat rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
tmpfs /run/user/1000 tmpfs rw,seclabel,nosuid,nodev,relatime,size=1596044k,mode=700,uid=1000,gid=1000 0 0
gvfsd-fuse /run/user/1000/gvfs fuse.gvfsd-fuse rw,nosuid,nodev,relatime,user_id=1000,group_id=1000 0 0
/dev/loop0 /mnt/iso iso9660 ro,relatime,nojoliet,check=s,map=n,blocksize=2048 0 0
/dev/loop0 /mnt/iso iso9660 ro,relatime,nojoliet,check=s,map=n,blocksize=2048 0 0
/dev/mapper/external-disk-root /mnt/external-disk ext4 ro,seclabel,relatime 0 0

0 comments on commit 6d0c396

Please sign in to comment.