__init__.py 5.71 KB
Newer Older
Mario Hock's avatar
Mario Hock committed
1
2
3
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

Mario Hock's avatar
Mario Hock committed
4
import os
Mario Hock's avatar
Mario Hock committed
5
6
import psutil
import time
Mario Hock's avatar
Mario Hock committed
7
8
import sys
import traceback
9
import math
Mario Hock's avatar
Mario Hock committed
10

11
12
13
from collections import namedtuple

import helpers
Mario Hock's avatar
Mario Hock committed
14
import curses_display as ui
15
16
from logging import LoggingManager

17
18
19
20
21

def get_time():
    """ Unified/comparable clock access """
    return time.time()

Mario Hock's avatar
Mario Hock committed
22
23
24
25
26
27
28
29
30

## XXX for interactive debugging only
def RELOAD():
    print ("import importlib")
    print ("importlib.reload(cpunetlog)")


MEASUREMENT_INTERVAL = 0.2

Mario Hock's avatar
Mario Hock committed
31
nic_speeds = helpers.get_nic_speeds()
32
nics = list( nic_speeds.keys() )
Mario Hock's avatar
Mario Hock committed
33

34
35

class Reading:
Mario Hock's avatar
Mario Hock committed
36
37
    """ A single reading of various CPU, NET, ... values. --> Building block for the »Measurement« class."""

Mario Hock's avatar
Mario Hock committed
38
39
    def __init__(self):
        ## * measurements *
40
        self.timestamp = get_time()
Mario Hock's avatar
Mario Hock committed
41
42
        #self.cpu_util = psutil.cpu_percent(interval=0, percpu=True)                      ## XXX
        #self.cpu_times_percent = psutil.cpu_times_percent(interval=0, percpu=True)       ## XXX
43
        self.cpu_times = psutil.cpu_times(percpu=True)
Mario Hock's avatar
Mario Hock committed
44
45
        self.memory = psutil.virtual_memory()
        self.net_io = psutil.net_io_counters(pernic=True)
Mario Hock's avatar
Mario Hock committed
46
47

    def __str__(self):
Mario Hock's avatar
Mario Hock committed
48
49
50
51
52
53
        ## •‣∘⁕∗◘☉☀★◾☞☛⦿
        return "◘ Timespan: " + str(self.timespan) +              \
                "\n◘ CPU utilization: " + str(self.cpu_util) +    \
                "\n◘ CPU times: " + str(self.cpu_times) +         \
                "\n◘ RAM: " + str(self.memory) +                  \
                "\n◘ NET: " + str(self.net_io)
Mario Hock's avatar
Mario Hock committed
54
55


56
57

class NetworkTraffic:
Mario Hock's avatar
Mario Hock committed
58
59
    """ Utility class for calculating and storing network traffic: Total amount (during a timespan) and ratio. """

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
    def __init__(self, older_counters, younger_counters, timespan):
        self.total = dict()
        self.ratio = dict()

        for field in older_counters._fields:
            field_delta = getattr(younger_counters, field) - getattr(older_counters, field)

            self.total[field] = field_delta
            self.ratio[field] = field_delta / timespan

    def __str__(self):
        return "Total (bytes):" + str(self.total) + "; Ratio (bytes/s)" + str(self.ratio)



class Measurement:
Mario Hock's avatar
Mario Hock committed
76
77
    """ Calculates and stores CPU utilization, network traffic, ... during a timespan. Based two »Readings«. """

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    def __init__(self, reading1, reading2):
        self.r1 = reading1
        self.r2 = reading2

        ## calculate differences
        self.timespan = self.r2.timestamp - self.r1.timestamp
        self.cpu_times_percent = helpers.calculate_cpu_times_percent(self.r1.cpu_times, self.r2.cpu_times, percpu=True)
        self.net_io = self._calculate_net_io()


    def _calculate_net_io(self):
        ret = dict()

        for nic in self.r1.net_io.keys():
            ret[nic] = NetworkTraffic(self.r1.net_io[nic], self.r2.net_io[nic], self.timespan)

        return ret



def measure(interval = MEASUREMENT_INTERVAL):
Mario Hock's avatar
Mario Hock committed
99
100
    """ Convenience function to perform one »Measurement« """

101
102
103
104
105
106
107
108
109
110
    r1 = Reading()
    time.sleep(interval)
    r2 = Reading()

    m = Measurement(r1, r2)

    return m



Mario Hock's avatar
Mario Hock committed
111

112
113
114
115
116
def main_loop():
    """ Main Loop:
      - Sets up curses-display
      - Takes a reading every second
      - Displays the measurements
117
      - Logs the measurements with the LoggingManager
118
119
    """

Mario Hock's avatar
Mario Hock committed
120
121
122
    err = None

    try:
123
        # Set up (curses) UI.
124
        ui.nics = nics
Mario Hock's avatar
Mario Hock committed
125
        ui.nic_speeds = nic_speeds
126
127
128
129
130
        ui.init()

        # Take an initial reading.
        old_reading = Reading()

131
132
133
134
        # Sleep till the next "full" second begins. (In order to roughly synchronize with other instances.)
        now = time.time()
        time.sleep(math.ceil(now)-now)

135
136
137
138
139
140
141
142
143
144
        running = True
        while running:
            # Take a new reading.
            new_reading = Reading()

            # Calculate the measurement from the last two readings.
            measurement = Measurement(old_reading, new_reading)

            # Display the measurement.
            running = ui.display( measurement )
145
            logging_manager.log(measurement)
146
147
148

            # Store the last reading as |old_reading|.
            old_reading = new_reading
Mario Hock's avatar
Mario Hock committed
149

150
151
152
153
            time.sleep(1)
                ## XXX We could calculating the remaining waiting-time here.
                #    (But I assume the difference is negligible.)

Mario Hock's avatar
Mario Hock committed
154
155

    except KeyboardInterrupt:
156
        # Quit gracefully on Ctrl-C
Mario Hock's avatar
Mario Hock committed
157
158
        pass
    except Exception as e:
159
        # On error: Store stack trace for later processing.
Mario Hock's avatar
Mario Hock committed
160
161
162
        err = e
        exc_type, exc_value, exc_traceback = sys.exc_info()
    finally:
163
        # Tear down the UI.
Mario Hock's avatar
Mario Hock committed
164
        ui.close()
165
        logging_manager.close()
Mario Hock's avatar
Mario Hock committed
166
167
168
169

    ## On error: Print error message *after* curses has quit.
    if ( err ):
        print( "Unexpected exception happened: '" + str(err) + "'" )
Mario Hock's avatar
Mario Hock committed
170
        print
171

Mario Hock's avatar
Mario Hock committed
172
173
174
175
176
        traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout)

        print
        print( "QUIT." )

177

178
179
180
181
182
183
184
185
186


## MAIN ##
if __name__ == "__main__":

    ## Command line arguments
    import argparse

    parser = argparse.ArgumentParser()
187
188
189
190
191
192
    parser.add_argument("-l", "--logging", action="store_true",
                        help="Enables logging.")
    parser.add_argument("-c", "--comment",
                        help="A comment that is stored in the logfile. (See --logging.)")
    parser.add_argument("--path", default="/tmp/cpunetlog",
                        help="Path where the log files are stored in. (See --logging.)")
193
194
    args = parser.parse_args()

Mario Hock's avatar
Mario Hock committed
195
196
197
198
199
200

    ## Get hostname (for display and logging).
    osdetails = tuple(os.uname())
    ostype = osdetails[0]
    hostname = osdetails[1]

201
    ## Logging
Mario Hock's avatar
Mario Hock committed
202
    logging_manager = LoggingManager( psutil.NUM_CPUS, nics, hostname, args.comment, args.path )
203
204
205
206
207
208
    if args.logging:
        logging_manager.enable_measurement_logger()


    # Run the main loop.
    main_loop()
Mario Hock's avatar
Mario Hock committed
209