OILS / regtest / stat_log.py View on Github | oils.pub

103 lines, 62 significant
1#!/usr/bin/env python3
2"""
3stat_log.py - Save portions of /proc/stat and /proc/vmstat
4
5Runs in an infinite loop, until SIGTERM
6"""
7import optparse
8import os
9import signal
10import sys
11import time
12
13
14def log(msg, *args):
15 if args:
16 msg = msg % args
17 print(msg, file=sys.stderr)
18
19
20def Options():
21 """Returns an option parser instance."""
22
23 p = optparse.OptionParser()
24
25 p.add_option('--out-dir',
26 dest='out_dir',
27 default='_tmp',
28 help='Write files to this directory')
29
30 p.add_option('--sleep-secs',
31 dest='sleep_secs',
32 type='int',
33 default=1,
34 help='Seconds to sleep')
35
36 return p
37
38
39o_stat = None
40o_vm = None
41
42
43def Handler(signum, frame):
44 log('[%s] Received SIGTERM, flushing logs and exiting ...', sys.argv[0])
45 o_stat.flush()
46 o_vm.flush()
47 sys.exit(0)
48
49
50def main(argv):
51 o = Options()
52 opts, argv = o.parse_args(argv)
53
54 # Saving lines
55 stat_filename = os.path.join(opts.out_dir, 'stat.txt')
56 vmstat_filename = os.path.join(opts.out_dir, 'vmstat.txt')
57
58 log('[%s] Saving to %s, %s every %d seconds', sys.argv[0], stat_filename,
59 vmstat_filename, opts.sleep_secs)
60
61 global o_stat, o_vm # flushed by signal handler
62
63 o_stat = open(stat_filename, 'w')
64 o_vm = open(vmstat_filename, 'w')
65
66 signal.signal(signal.SIGTERM, Handler)
67
68 i = 0
69 while True:
70 t = int(time.time()) # truncate to nearest second, to save space
71 #print(t)
72
73 with open('/proc/stat') as i_stat:
74 for line in i_stat:
75 # context switches
76 if line.startswith('cpu') or line.startswith('ctx'):
77 #log('line %r', line)
78 o_stat.write('%s %s' % (t, line))
79
80 with open('/proc/vmstat') as i_stat:
81 for line in i_stat:
82 # context switches
83 if line.startswith('pgfault') or line.startswith('pgmajfault'):
84 #log('line %r', line)
85 o_vm.write('%s %s' % (t, line))
86
87 # So we can tail -f
88 if i % 10 == 0:
89 o_stat.flush()
90 o_vm.flush()
91
92 time.sleep(opts.sleep_secs)
93 i += 1
94
95 return 0
96
97
98if __name__ == '__main__':
99 try:
100 sys.exit(main(sys.argv))
101 except RuntimeError as e:
102 print('FATAL: %s' % e, file=sys.stderr)
103 sys.exit(1)