further the sensors example to load and parse files
This commit is contained in:
		@@ -5,6 +5,38 @@
 | 
			
		||||
#include <boost/function.hpp>
 | 
			
		||||
#include <boost/date_time/posix_time/posix_time.hpp>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
std::string load_text_file(const std::string &filename)
 | 
			
		||||
{
 | 
			
		||||
  std::ifstream infile(filename.c_str());
 | 
			
		||||
 | 
			
		||||
  std::string str;
 | 
			
		||||
 | 
			
		||||
  std::string result;
 | 
			
		||||
 | 
			
		||||
  while (std::getline(infile, str))
 | 
			
		||||
  {
 | 
			
		||||
    result += str + "\n";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::vector<dispatchkit::Boxed_Value> regex_search(const std::string &str, const std::string ®ex)
 | 
			
		||||
{
 | 
			
		||||
  boost::smatch matches;
 | 
			
		||||
  boost::regex_search(str, matches, boost::regex(regex));
 | 
			
		||||
 | 
			
		||||
  std::vector<dispatchkit::Boxed_Value> results;
 | 
			
		||||
 | 
			
		||||
  for (unsigned int i = 0; i < matches.size(); ++i)
 | 
			
		||||
  {
 | 
			
		||||
    results.push_back(dispatchkit::Boxed_Value(std::string(matches.str(i))));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return results;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct Sensor_Manager
 | 
			
		||||
{
 | 
			
		||||
  struct Sensor
 | 
			
		||||
@@ -76,7 +108,12 @@ int main(int argc, char *argv[]) {
 | 
			
		||||
 | 
			
		||||
  Sensor_Manager sensor_manager;
 | 
			
		||||
  chai.get_eval_engine().add_object("sensor_manager", boost::ref(sensor_manager));
 | 
			
		||||
 | 
			
		||||
  dispatchkit::register_function(chai.get_eval_engine(), &Sensor_Manager::add_sensor, "add_sensor");
 | 
			
		||||
  dispatchkit::register_function(chai.get_eval_engine(), ®ex_search, "regex_search");
 | 
			
		||||
  dispatchkit::register_function(chai.get_eval_engine(), &load_text_file, "load_text_file");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  for (int i = 1; i < argc; ++i) {
 | 
			
		||||
    try {
 | 
			
		||||
 
 | 
			
		||||
@@ -347,6 +347,11 @@ namespace dispatchkit
 | 
			
		||||
    Boxed_POD_Value(const Boxed_Value &v)
 | 
			
		||||
      : d(0), i(0), m_isfloat(false)
 | 
			
		||||
    {
 | 
			
		||||
      if (!v.get_type_info().m_type_info)
 | 
			
		||||
      {
 | 
			
		||||
        throw boost::bad_any_cast();
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      const std::type_info &inp_ = *v.get_type_info().m_type_info;
 | 
			
		||||
 | 
			
		||||
      if (inp_ == typeid(double))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,14 +1,66 @@
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
}                                                                     
 | 
			
		||||
 | 
			
		||||
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");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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, function(state) { update_state(state); state["cpu"]; } )
 | 
			
		||||
sensor_manager.add_sensor("cpu0", 500, global_state, function(state) { state["cpu0"]; } )
 | 
			
		||||
sensor_manager.add_sensor("cpu1", 500, global_state, function(state) { state["cpu1"]; } )
 | 
			
		||||
 | 
			
		||||
global_state["CPU"] = 0.0;
 | 
			
		||||
global_state["CPU0"] = 0.0;
 | 
			
		||||
                                                    
 | 
			
		||||
def update_state(state, name)
 | 
			
		||||
{
 | 
			
		||||
  ++state[name];
 | 
			
		||||
}                                                                     
 | 
			
		||||
 | 
			
		||||
sensor_manager.add_sensor("CPU", 1000, global_state, function(state) { update_state(state, "CPU"); state["CPU"]; } )
 | 
			
		||||
sensor_manager.add_sensor("CPU0", 1000, global_state, function(state) { update_state(state, "CPU0"); state["CPU0"]; } )
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user