|
9 | 9 | import time |
10 | 10 | import logging |
11 | 11 |
|
| 12 | +def runcmd(cmd): |
| 13 | + logger = logging.getLogger(__name__) |
| 14 | + process=subprocess.Popen(cmd.split(),stdout=subprocess.PIPE) |
| 15 | + out,err=process.communicate() |
| 16 | + logger.info(out) |
| 17 | + return out |
| 18 | + |
12 | 19 | def monitor(): |
13 | 20 | logger = logging.getLogger(__name__) |
14 | 21 | while True: |
15 | | - cmdlist=['df -h', 'df -i -h','vmstat -a -SM 1 1', 'iostat'] |
16 | | - for cmd in cmdlist: |
17 | | - process=subprocess.Popen(cmd.split(),stdout=subprocess.PIPE) |
18 | | - out,err=process.communicate() |
19 | | - logger.info(out) |
| 22 | + out = runcmd('df -h') |
| 23 | + metrics = str(out).split('\\n')[1] |
| 24 | + metrics = [x for x in metrics.split(' ') if x] |
| 25 | + logger.info(f'Root disk usage {metrics[4]}.') |
| 26 | + if int((metrics[4].split('%')[0]))> 90: |
| 27 | + logger.warning('WARNING: High disk usage.') |
| 28 | + |
| 29 | + runcmd('df -i -h') |
| 30 | + |
| 31 | + out = runcmd('vmstat -a -SM 1 1') |
| 32 | + metrics = str(out).split('\\n')[2] |
| 33 | + metrics = [x for x in metrics.split(' ') if x] |
| 34 | + logger.info(f'Free memory {metrics[3]}. Inactive memory {metrics[4]}. Active memory {metrics[5]}') |
| 35 | + if float(metrics[4])/float(metrics[5]) < .1: |
| 36 | + logger.warning('WARNING: High memory usage.') |
| 37 | + |
| 38 | + out = runcmd('iostat') |
| 39 | + metrics = str(out).split('\\n')[3] |
| 40 | + metrics = [x for x in metrics.split(' ') if x] |
| 41 | + logger.info(f'Idle CPU {metrics[-1]}') |
| 42 | + if float(metrics[-1]) < 10: |
| 43 | + logger.warning('WARNING: Low CPU') |
20 | 44 | time.sleep(30) |
21 | 45 |
|
22 | 46 | if __name__=='__main__': |
|
0 commit comments