lib/cpee/implementation.rb in cpee-2.1.32 vs lib/cpee/implementation.rb in cpee-2.1.33

- old
+ new

@@ -66,10 +66,13 @@ opts[:states] ||= File.expand_path(File.join(__dir__,'..','..','server','resources','states.xml')) opts[:watchdog_frequency] ||= 7 opts[:watchdog_start_off] ||= false opts[:infinite_loop_stop] ||= 10000 + opts[:dashing_frequency] ||= 3 + opts[:dashing_target] ||= nil + ### set redis_cmd to nil if you want to do global ### at least redis_path or redis_url and redis_db have to be set if you do global opts[:redis_path] ||= 'redis.sock' # use e.g. /tmp/redis.sock for global stuff. Look it up in your redis config opts[:redis_db] ||= 0 ### optional redis stuff @@ -111,9 +114,58 @@ EM.defer do ### catch all sse connections CPEE::Notifications::sse_distributor(opts) end EM.add_periodic_timer(opts[:sse_keepalive_frequency]) do CPEE::Notifications::sse_heartbeat(opts) + end + + if opts[:dashing_target] + cpu_last = 0 + idl_last = 0 + EM.add_periodic_timer(opts[:dashing_frequency]) do + src = `cat /proc/stat | head -n 1`.split("\n") + srm = `cat /proc/meminfo`.split("\n") + sc = {} + sm = {} + src.each do |e| + x = e.split(' ') + sc[x[0]] = x[1..-1].map{|r| r.to_i} + end + srm.each do |e| + x = e.split(/\s+/) + sm[x[0].chop] = x[1].to_i + end + scc = 0 + sci = 0 + sc.each do |_,e| + scc = e[0..4].sum + sci = e[3] + end + cpu_delta = scc - cpu_last + cpu_idle = sci - idl_last + cpu_used = cpu_delta - cpu_idle + cpu_usage = '%.2f' % (100 * cpu_used / cpu_delta.to_f) + mem_tot = '%.1f' % (sm['MemTotal']/1024.0) + mem_fre = '%.1f' % (sm['MemFree']/1024.0) + mem_ava = '%.1f' % (sm['MemAvailable']/1024.0) + mem_buc = '%.1f' % ((sm['Buffers'] + sm['Cached'] + sm['SReclaimable'])/1024.0) + mem_usd = '%.1f' % ((sm['MemTotal'] - sm['MemFree'] - sm['Buffers'] - sm['Cached'] - sm['SReclaimable'])/1024.0) + + # puts "CPU usage at #{cpu_usage}%" + # puts "Mem usage at #{mem_tot}/#{mem_fre}/#{mem_usd}/#{mem_buc}/#{mem_ava}" + content = {} + content['cpu_usage'] = cpu_usage + content['mem_total'] = mem_tot + content['mem_free'] = mem_fre + content['mem_available'] = mem_ava + content['mem_bufferedandcached'] = mem_buc + content['mem_used'] = mem_usd + CPEE::Message::send_url(:event,'node/resource_utilization',File.join(opts[:url],'/'),content,File.join(opts[:dashing_target],'/dash/events')) + + # Keep this as last for our next read + idl_last = sci + cpu_last = scc + end end end cleanup do CPEE::cleanup_services(opts[:watchdog_start_off])