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