Permalink
Browse files

Merge pull request #414 from rohitjalan/master

Passenger Stats Added
  • Loading branch information...
2 parents 0a831fa + 1e83867 commit 73329e33da1e329093934a74ae085d806fd01fd7 @kormoc kormoc committed Jul 19, 2013
@@ -0,0 +1,127 @@
+# coding=utf-8
+
+"""
+The PasengerCollector collects CPU utilization of apache,nginx and passenger process
+It also captures memory utilized of passenger, apache and nginx processes
+Four key attributes to be published - phusion_passenger_cpu, total_apache_memory, total_passenger_mem, total_nginx_mem
+
+To utilize this function, run diamond as root user
+#### Dependencies
+
+ * passenger-memory-stats
+
+"""
+import diamond.collector
+import os
+import time
+import re
+import subprocess
+from diamond.collector import str_to_bool
+from cStringIO import StringIO
+
+class PassengerCollector(diamond.collector.Collector):
+
+ def get_default_config_help(self):
+ config_help = super(PassengerCollector, self).get_default_config_help()
+ config_help.update({
+
+ })
+ return config_help
+
+ def get_default_config(self):
+ """
+ Returns the default collector settings
+ """
+ config = super(PassengerCollector, self).get_default_config()
+ config.update({
+ 'path': 'passenger_stats',
+ })
+ return config
+
+ def collect(self):
+ """
+ Collector Passenger stats
+ """
+ def calc_cpu(processes):
+ pipe1 = "top -b -n 2"
+ pipe2 = "egrep " + processes
+ pipe3 = "awk {print $9}"
+ p1 = subprocess.Popen(pipe1.split(), stdout=subprocess.PIPE)
+ p2 = subprocess.Popen(pipe2.split(), stdin=p1.stdout, stdout=subprocess.PIPE)
+ p3 = subprocess.Popen(pipe3.split(" ",1), stdin=p2.stdout, stdout=subprocess.PIPE)
+ result = p3.communicate()
+ cpu_usage = result[0].split('\n')[:-1]
+ total_cpu = 0.0
+ for index in range(len(cpu_usage)/2, len(cpu_usage)):
+ cpu = float(cpu_usage[index])
+ total_cpu += cpu
+
+ return str(total_cpu)
+
+ k1 = "passenger-memory-stats"
+ k2 = 'sed -r s/\x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]//g'
+ q1 = subprocess.Popen(k1.split(), stdout=subprocess.PIPE)
+ q2 = subprocess.Popen(k2.split(), stdin=q1.stdout, stdout=subprocess.PIPE)
+ (res,err) = q2.communicate()
+ f = StringIO(res)
+ passenger_flag = 0
+ apache_flag = 0
+ nginx_flag = 0
+
+ apache_processes = ""
+ nginx_processes = ""
+ passenger_processes = ""
+
+ total_passenger_mem = 0.0
+ total_apache_mem = 0.0
+ total_nginx_mem = 0.0
+ for line in f:
+ if('Passenger processes' in line):
+ passenger_flag = 1
+ continue
+ if('Apache processes' in line):
+ apache_flag = 1
+ continue
+ if('Nginx processes' in line):
+ nginx_flag = 1
+ continue
+
+ #If line starts with digit, then store process ids and memory
+ matchObj = re.match(r"^\d", line)
+ if matchObj:
+ processList = line.split()
+ if(apache_flag == 1):
+ apache_processes += (processList[0] + "|")
+ total_apache_mem += float(processList[4])
+ if(passenger_flag == 1):
+ passenger_processes += (processList[0] + "|")
+ total_passenger_mem += float(processList[3])
+ if(nginx_flag == 1):
+ nginx_processes += (processList[0] + "|")
+ total_nginx_mem += float(processList[4])
+
+ elif('Processes:' in line):
+ passenger_flag = 0
+ apache_flag = 0
+ nginx_flag = 0
+ apache_processes = apache_processes[:-1]
+ nginx_processes = nginx_processes[:-1]
+ passenger_processes = passenger_processes[:-1]
+
+ #calculate cpu of apache, nginx and passenger processes
+ overall_cpu = 0
+ if apache_processes:
+ apache_cpu = calc_cpu(apache_processes)
+ overall_cpu += float(apache_cpu)
+ if nginx_processes:
+ nginx_cpu = calc_cpu(nginx_processes)
+ overall_cpu += float(nginx_cpu)
+ if passenger_processes:
+ passenger_cpu = calc_cpu(passenger_processes)
+ overall_cpu += float(passenger_cpu)
+
+ self.publish('phusion_passenger_cpu', overall_cpu)
+ self.publish('total_apache_memory', total_apache_mem)
+ self.publish('total_passenger_mem', total_passenger_mem)
+ self.publish('total_nginx_mem', total_nginx_mem)
+
@@ -0,0 +1,34 @@
+#!/usr/bin/python
+# coding=utf-8
+################################################################################
+
+from test import CollectorTestCase
+from test import get_collector_config
+from test import unittest
+from mock import Mock
+from mock import patch
+
+try:
+ from cStringIO import StringIO
+ StringIO # workaround for pyflakes issue #13
+except ImportError:
+ from StringIO import StringIO
+
+from diamond.collector import Collector
+from passenger_stats import PassengerCollector
+
+################################################################################
+
+class TestPassengerCollector(CollectorTestCase):
+ def setUp(self):
+ config = get_collector_config('PassengerCollector',{})
+ self.collector = PassengerCollector(config, None)
+
+
+ def test_import(self):
+ self.assertTrue(PassengerCollector)
+
+################################################################################
+if __name__ == "__main__":
+ unittest.main()
+

0 comments on commit 73329e3

Please sign in to comment.