|
| 1 | +#!/usr/bin/env python |
| 2 | +# This file is part of tcollector. |
| 3 | +# Copyright (C) 2012-2024 The tcollector Authors. |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify it |
| 6 | +# under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or (at your |
| 8 | +# option) any later version. This program is distributed in the hope that it |
| 9 | +# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 10 | +# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser |
| 11 | +# General Public License for more details. You should have received a copy |
| 12 | +# of the GNU Lesser General Public License along with this program. If not, |
| 13 | +# see <http://www.gnu.org/licenses/>. |
| 14 | +# |
| 15 | + |
| 16 | +""" |
| 17 | +CPU detailed statistics for TSDB |
| 18 | +
|
| 19 | +This plugin tracks, for all CPUs: |
| 20 | +
|
| 21 | +- user % |
| 22 | +- nice % |
| 23 | +- system % |
| 24 | +- interrupt % |
| 25 | +- idle % |
| 26 | +
|
| 27 | +Requirements : |
| 28 | +- FreeBSD : top |
| 29 | +- Linux : mpstat |
| 30 | +
|
| 31 | +In addition, for FreeBSD, it reports : |
| 32 | +- load average (1m, 5m, 15m) |
| 33 | +- number of processes (total, starting, running, sleeping, stopped, zombie, waiting, lock) |
| 34 | +- memory statistics (bytes) (active, inact, wired, cache, buf, free) |
| 35 | +- arc statistics (bytes) (total, mru, mfu, anon, header, other) |
| 36 | +- swap statistics (bytes) (total, free, inuse, in/s, out/s) |
| 37 | +""" |
| 38 | +# TODO: refactor - remove linux specific code |
| 39 | + |
| 40 | +import errno |
| 41 | +import sys |
| 42 | +import time |
| 43 | +import subprocess |
| 44 | +import re |
| 45 | +import signal |
| 46 | +import os |
| 47 | + |
| 48 | +from collectors.lib import utils |
| 49 | + |
| 50 | + |
| 51 | +try: |
| 52 | + from collectors.etc import sysload_conf |
| 53 | +except ImportError: |
| 54 | + sysload_conf = None |
| 55 | + |
| 56 | +DEFAULT_COLLECTION_INTERVAL = 15 |
| 57 | + |
| 58 | + |
| 59 | +def convert_to_bytes(string): |
| 60 | + """Take a string in the form 1234K, and convert to bytes""" |
| 61 | + factors = { |
| 62 | + "K": 1024, |
| 63 | + "M": 1024 * 1024, |
| 64 | + "G": 1024 * 1024 * 1024, |
| 65 | + "T": 1024 * 1024 * 1024 * 1024, |
| 66 | + "P": 1024 * 1024 * 1024 * 1024 * 1024, |
| 67 | + "E": 1024 * 1024 * 1024 * 1024 * 1024 * 1024, |
| 68 | + } |
| 69 | + for f, fm in factors.items(): |
| 70 | + if string.endswith(f): |
| 71 | + number = float(string[:-1]) |
| 72 | + number = number * fm |
| 73 | + return int(number) |
| 74 | + return int(string) |
| 75 | + |
| 76 | + |
| 77 | +signal_received = None |
| 78 | +def handlesignal(signum, stack): |
| 79 | + global signal_received |
| 80 | + signal_received = signum |
| 81 | + |
| 82 | + |
| 83 | +def main(): |
| 84 | + """top main loop""" |
| 85 | + |
| 86 | + collection_interval = DEFAULT_COLLECTION_INTERVAL |
| 87 | + collect_every_cpu = True |
| 88 | + if sysload_conf: |
| 89 | + config = sysload_conf.get_config() |
| 90 | + collection_interval = config['collection_interval'] |
| 91 | + collect_every_cpu = config['collect_every_cpu'] |
| 92 | + |
| 93 | + global signal_received |
| 94 | + |
| 95 | + signal.signal(signal.SIGTERM, handlesignal) |
| 96 | + signal.signal(signal.SIGINT, handlesignal) |
| 97 | + |
| 98 | + try: |
| 99 | + if collect_every_cpu: |
| 100 | + p_top = subprocess.Popen( |
| 101 | + ["mpstat", "-P", "ALL", str(collection_interval)], |
| 102 | + stdout=subprocess.PIPE, |
| 103 | + ) |
| 104 | + else: |
| 105 | + p_top = subprocess.Popen( |
| 106 | + ["mpstat", str(collection_interval)], |
| 107 | + stdout=subprocess.PIPE, |
| 108 | + ) |
| 109 | + except OSError as e: |
| 110 | + if e.errno == errno.ENOENT: |
| 111 | + # it makes no sense to run this collector here |
| 112 | + sys.exit(13) # we signal tcollector to not run us |
| 113 | + raise |
| 114 | + |
| 115 | + timestamp = 0 |
| 116 | + |
| 117 | + while signal_received is None: |
| 118 | + try: |
| 119 | + line = p_top.stdout.readline() |
| 120 | + except (IOError, OSError) as e: |
| 121 | + if e.errno in (errno.EINTR, errno.EAGAIN): |
| 122 | + break |
| 123 | + raise |
| 124 | + |
| 125 | + if not line: |
| 126 | + # end of the program, die |
| 127 | + break |
| 128 | + |
| 129 | + # CPU: --> CPU all: : FreeBSD, to match the all CPU |
| 130 | + # %( [uni][a-z]+,?)? : FreeBSD, so that top output matches mpstat output |
| 131 | + # AM : Linux, mpstat output depending on locale |
| 132 | + # PM : Linux, mpstat output depending on locale |
| 133 | + # .* load : FreeBSD, to correctly match load averages |
| 134 | + # , : FreeBSD, to correctly match processes: Mem: ARC: and Swap: |
| 135 | + fields = re.sub("CPU:", "CPU all:", re.sub(r"%( [uni][a-z]+,?)?| AM | PM |.* load |,", "", line)).split() |
| 136 | + if len(fields) <= 0: |
| 137 | + continue |
| 138 | + |
| 139 | + if (((fields[0] == "CPU") or (re.match("[0-9][0-9]:[0-9][0-9]:[0-9][0-9]",fields[0]))) and ((collect_every_cpu and re.match("[0-9]+:?",fields[1])) or ((not collect_every_cpu) and re.match("all:?",fields[1])))): |
| 140 | + if((fields[1] == "all") or (fields[1] == "0")): |
| 141 | + timestamp = int(time.time()) |
| 142 | + cpuid = fields[1].replace(":","") |
| 143 | + cpuuser = fields[2] |
| 144 | + cpunice = fields[3] |
| 145 | + cpusystem = fields[4] |
| 146 | + cpuinterrupt = fields[6] |
| 147 | + cpuidle = fields[-1] |
| 148 | + print("cpu.usr %s %s cpu=%s" % (timestamp, float(cpuuser), cpuid)) |
| 149 | + print("cpu.nice %s %s cpu=%s" % (timestamp, float(cpunice), cpuid)) |
| 150 | + print("cpu.sys %s %s cpu=%s" % (timestamp, float(cpusystem), cpuid)) |
| 151 | + print("cpu.irq %s %s cpu=%s" % (timestamp, float(cpuinterrupt), cpuid)) |
| 152 | + print("cpu.idle %s %s cpu=%s" % (timestamp, float(cpuidle), cpuid)) |
| 153 | + |
| 154 | + elif fields[0] == "averages:": |
| 155 | + timestamp = int(time.time()) |
| 156 | + print("load.1m %s %s" % (timestamp, fields[1])) |
| 157 | + print("load.5m %s %s" % (timestamp, fields[2])) |
| 158 | + print("load.15m %s %s" % (timestamp, fields[3])) |
| 159 | + |
| 160 | + elif re.match("[0-9]+ processes:",line): |
| 161 | + starting = 0 |
| 162 | + running = 0 |
| 163 | + sleeping = 0 |
| 164 | + stopped = 0 |
| 165 | + zombie = 0 |
| 166 | + waiting = 0 |
| 167 | + lock = 0 |
| 168 | + for i in range(len(fields)): |
| 169 | + if fields[i] == "starting": |
| 170 | + starting = fields[i-1] |
| 171 | + if fields[i] == "running": |
| 172 | + running = fields[i-1] |
| 173 | + if fields[i] == "sleeping": |
| 174 | + sleeping = fields[i-1] |
| 175 | + if fields[i] == "stopped": |
| 176 | + stopped = fields[i-1] |
| 177 | + if fields[i] == "zombie": |
| 178 | + zombie = fields[i-1] |
| 179 | + if fields[i] == "waiting": |
| 180 | + waiting = fields[i-1] |
| 181 | + if fields[i] == "lock": |
| 182 | + lock = fields[i-1] |
| 183 | + print("ps.all %s %s" % (timestamp, fields[0])) |
| 184 | + print("ps.start %s %s" % (timestamp, starting)) |
| 185 | + print("ps.run %s %s" % (timestamp, running)) |
| 186 | + print("ps.sleep %s %s" % (timestamp, sleeping)) |
| 187 | + print("ps.stop %s %s" % (timestamp, stopped)) |
| 188 | + print("ps.zomb %s %s" % (timestamp, zombie)) |
| 189 | + print("ps.wait %s %s" % (timestamp, waiting)) |
| 190 | + print("ps.lock %s %s" % (timestamp, lock)) |
| 191 | + |
| 192 | + elif fields[0] == "Mem:": |
| 193 | + active = 0 |
| 194 | + inact = 0 |
| 195 | + wired = 0 |
| 196 | + cache = 0 |
| 197 | + buf = 0 |
| 198 | + free = 0 |
| 199 | + for i in range(len(fields)): |
| 200 | + if fields[i] == "Active": |
| 201 | + active = convert_to_bytes(fields[i-1]) |
| 202 | + if fields[i] == "Inact": |
| 203 | + inact = convert_to_bytes(fields[i-1]) |
| 204 | + if fields[i] == "Wired": |
| 205 | + wired = convert_to_bytes(fields[i-1]) |
| 206 | + if fields[i] == "Cache": |
| 207 | + cache = convert_to_bytes(fields[i-1]) |
| 208 | + if fields[i] == "Buf": |
| 209 | + buf = convert_to_bytes(fields[i-1]) |
| 210 | + if fields[i] == "Free": |
| 211 | + free = convert_to_bytes(fields[i-1]) |
| 212 | + print("mem.active %s %s" % (timestamp, active)) |
| 213 | + print("mem.inact %s %s" % (timestamp, inact)) |
| 214 | + print("mem.wired %s %s" % (timestamp, wired)) |
| 215 | + print("mem.cache %s %s" % (timestamp, cache)) |
| 216 | + print("mem.buf %s %s" % (timestamp, buf)) |
| 217 | + print("mem.free %s %s" % (timestamp, free)) |
| 218 | + |
| 219 | + elif fields[0] == "ARC:": |
| 220 | + total = 0 |
| 221 | + mru = 0 |
| 222 | + mfu = 0 |
| 223 | + anon = 0 |
| 224 | + header = 0 |
| 225 | + other = 0 |
| 226 | + for i in range(len(fields)): |
| 227 | + if (fields[i] == "Total"): |
| 228 | + total=convert_to_bytes(fields[i-1]) |
| 229 | + if(fields[i] == "MRU"): |
| 230 | + mru=convert_to_bytes(fields[i-1]) |
| 231 | + if(fields[i] == "MFU"): |
| 232 | + mfu=convert_to_bytes(fields[i-1]) |
| 233 | + if(fields[i] == "Anon"): |
| 234 | + anon=convert_to_bytes(fields[i-1]) |
| 235 | + if(fields[i] == "Header"): |
| 236 | + header=convert_to_bytes(fields[i-1]) |
| 237 | + if(fields[i] == "Other"): |
| 238 | + other=convert_to_bytes(fields[i-1]) |
| 239 | + print("arc.total %s %s" % (timestamp, total)) |
| 240 | + print("arc.mru %s %s" % (timestamp, mru)) |
| 241 | + print("arc.mfu %s %s" % (timestamp, mfu)) |
| 242 | + print("arc.anon %s %s" % (timestamp, anon)) |
| 243 | + print("arc.header %s %s" % (timestamp, header)) |
| 244 | + print("arc.other %s %s" % (timestamp, other)) |
| 245 | + |
| 246 | + elif(fields[0] == "Swap:"): |
| 247 | + total=0 |
| 248 | + used=0 |
| 249 | + free=0 |
| 250 | + inuse=0 |
| 251 | + inps=0 |
| 252 | + outps=0 |
| 253 | + for i in range(len(fields)): |
| 254 | + if(fields[i] == "Total"): |
| 255 | + total=convert_to_bytes(fields[i-1]) |
| 256 | + if(fields[i] == "Used"): |
| 257 | + used=convert_to_bytes(fields[i-1]) |
| 258 | + if(fields[i] == "Free"): |
| 259 | + free=convert_to_bytes(fields[i-1]) |
| 260 | + if(fields[i] == "Inuse"): |
| 261 | + inuse=convert_to_bytes(fields[i-1]) |
| 262 | + if(fields[i] == "In"): |
| 263 | + inps=convert_to_bytes(fields[i-1])/collection_interval |
| 264 | + if(fields[i] == "Out"): |
| 265 | + outps=convert_to_bytes(fields[i-1])/collection_interval |
| 266 | + print("swap.total %s %s" % (timestamp, total)) |
| 267 | + print("swap.used %s %s" % (timestamp, used)) |
| 268 | + print("swap.free %s %s" % (timestamp, free)) |
| 269 | + print("swap.inuse %s %s" % (timestamp, inuse)) |
| 270 | + print("swap.inps %s %s" % (timestamp, inps)) |
| 271 | + print("swap.outps %s %s" % (timestamp, outps)) |
| 272 | + |
| 273 | + sys.stdout.flush() |
| 274 | + |
| 275 | + if signal_received is None: |
| 276 | + signal_received = signal.SIGTERM |
| 277 | + try: |
| 278 | + os.kill(p_top.pid, signal_received) |
| 279 | + except Exception: |
| 280 | + pass |
| 281 | + p_top.wait() |
| 282 | + |
| 283 | + |
| 284 | +if __name__ == "__main__": |
| 285 | + sys.exit(main()) |
0 commit comments