71 lines
2.2 KiB
ChaiScript
71 lines
2.2 KiB
ChaiScript
|
|
|
|
def initialize_cpu_sensor(state, cpuname, sensor_manager)
|
|
{
|
|
state[cpuname] = 0.0;
|
|
state[cpuname + ".user"] = 0.0;
|
|
state[cpuname + ".nice"] = 0.0;
|
|
state[cpuname + ".system"] = 0.0;
|
|
state[cpuname + ".idle"] = 0.0;
|
|
state[cpuname + ".iowait"] = 0.0;
|
|
state[cpuname + ".irq"] = 0.0;
|
|
state[cpuname + ".softirq"] = 0.0;
|
|
}
|
|
|
|
def update_cpu_state(state, statfile, cpuname)
|
|
{
|
|
var regex = cpuname + "\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)\\s+([0-9]+)";
|
|
var strs = regex_search(statfile, regex);
|
|
|
|
var user = to_double(strs[1]);
|
|
var nice = to_double(strs[2]);
|
|
var system = to_double(strs[3]);
|
|
var idle = to_double(strs[4]);
|
|
var iowait = to_double(strs[5]);
|
|
var irq = to_double(strs[6]);
|
|
var softirq = to_double(strs[7]);
|
|
|
|
var userd = user - state[cpuname + ".user"];
|
|
var niced = nice - state[cpuname + ".nice"];
|
|
var systemd = system - state[cpuname + ".system"];
|
|
var idled = idle - state[cpuname + ".idle"];
|
|
var iowaitd = iowait - state[cpuname + ".iowait"];
|
|
var irqd = irq - state[cpuname + ".irq"];
|
|
var softirqd = softirq - state[cpuname + ".softirq"];
|
|
|
|
var totalticks = userd + niced + systemd + idled + iowaitd + irqd + softirqd;
|
|
|
|
state[cpuname] = (totalticks - idled) / totalticks
|
|
|
|
state[cpuname + ".user"] = user;
|
|
state[cpuname + ".nice"] = nice;
|
|
state[cpuname + ".system"] = system;
|
|
state[cpuname + ".idle"] = idle;
|
|
state[cpuname + ".iowait"] = iowait;
|
|
state[cpuname + ".irq"] = irq;
|
|
state[cpuname + ".softirq"] = softirq;
|
|
}
|
|
|
|
# annotation
|
|
def update_state(state)
|
|
{
|
|
var file = load_text_file("/proc/stat");
|
|
|
|
update_cpu_state(state, file, "cpu");
|
|
update_cpu_state(state, file, "cpu0");
|
|
update_cpu_state(state, file, "cpu1");
|
|
}
|
|
|
|
//dump_system()
|
|
|
|
var global_state = Map()
|
|
|
|
initialize_cpu_sensor(global_state, "cpu", sensor_manager);
|
|
initialize_cpu_sensor(global_state, "cpu0", sensor_manager);
|
|
initialize_cpu_sensor(global_state, "cpu1", sensor_manager);
|
|
|
|
sensor_manager.add_sensor("cpu", 500, global_state, fun(state) { update_state(state); state["cpu"]; } )
|
|
sensor_manager.add_sensor("cpu0", 500, global_state, fun(state) { state["cpu0"]; } )
|
|
sensor_manager.add_sensor("cpu1", 500, global_state, fun(state) { state["cpu1"]; } )
|
|
|