add motd generator script
[freebsd-maintainer-scripts/.git] / sysctl-missing-desc.py
CommitLineData
a43e03ab
SB
1#!/usr/bin/env python
2
3"""
4Detects missing sysctl's description and report them
5
6--
7sbz
8"""
9
10import subprocess
11import shlex
12import sys
13
14from pprint import pprint as pp
15
16
17SYSCTL_PREFIX = '/sbin/'
18SYSCTL_CMD = '{}sysctl'.format(SYSCTL_PREFIX)
19SYSCTL_ARGS = '-a -d'
20
21
22def sysctl_desc_empty(desc):
23 return desc == ' '
24
25
26def sysctl_summary(*sysctls):
27 count = {}
28 # sysctl -ad | grep ': $'|cut -d '.' -f1|sort -u | xargs echo
29 # compat debug dev hw kern kstat net p1003_1b security user vfs vm
30 categories = ['compat', 'debug', 'dev', 'hw', 'kern', 'kstat', 'net',
31 'machdep', 'p1003_1b', 'security', 'user', 'vfs', 'vm']
32 for c in categories:
33 count[c] = {}
34 count[c]['count'] = 0
35 count[c]['oids'] = []
36
37 for s in sysctls:
38 for c in categories:
39 if s.startswith(c):
40 count[c]['count'] += 1
41 count[c]['oids'].append(s)
42 count[c]['oids'] = sorted(count[c]['oids'])
43
44 return count
45
46
47def main():
48 total = 0
49 sysctl_to_fix = []
50
51 p = subprocess.Popen(shlex.split("{} {}".format(SYSCTL_CMD, SYSCTL_ARGS)),
52 stdout=subprocess.PIPE, universal_newlines=True)
53 stdout, stderr = p.communicate()
54
55 for sysctl_line in str(stdout).splitlines():
56 total += 1
57 if ':' in sysctl_line:
58 sysctl_oid, sysctl_desc = sysctl_line.split(':', 1)
59 if sysctl_desc_empty(sysctl_desc):
60 sysctl_to_fix.append(sysctl_oid)
61 #print (sysctl_oid, sysctl_desc)
62
63 pp(sysctl_summary(*sysctl_to_fix))
64 print("Number of total sysctl description to fix: {}/{} ({}%)".format(
65 len(sysctl_to_fix), total, len(sysctl_to_fix)/total*100))
66
67 return 0
68
69if __name__ == '__main__':
70 sys.exit(main())