Completed
Pull Request — master (#2304)
by Arma
07:07
created

apply_before()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 2
rs 10
cc 1
1
#!/usr/bin/python
2
3
import sys
4
5
6
def print_load_avg(args):
7
    period = args[1]
8
9
    loadavg_file = '/proc/loadavg'
10
    cpuinfo_file = '/proc/cpuinfo'
11
    cpus = 0
12
13
    try:
14
        fh = open(loadavg_file, 'r')
15
        load = fh.readline().split()[0:3]
16
        fh.close()
17
    except:
18
        sys.stderr.write('Error opening %s\n' % loadavg_file)
19
        sys.exit(2)
20
21
    try:
22
        fh = open(cpuinfo_file, 'r')
23
        for line in fh:
24
            if 'processor' in line:
25
                cpus += 1
26
        fh.close()
27
    except:
28
        sys.stderr.write('Error opeing %s\n' % cpuinfo_file)
29
30
    one_min = '1 min load/core: %s' % str(float(load[0]) / cpus)
31
    five_min = '5 min load/core: %s' % str(float(load[1]) / cpus)
32
    fifteen_min = '15 min load/core: %s' % str(float(load[2]) / cpus)
33
34
    if period == '1' or period == 'one':
35
        print(one_min)
36
    elif period == '5' or period == 'five':
37
        print(five_min)
38
    elif period == '15' or period == 'fifteen':
39
        print(fifteen_min)
40
    else:
41
        print(one_min + " " + five_min + " " + fifteen_min)
42
43
44
if __name__ == '__main__':
45
    print_load_avg(sys.argv)
46