Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.41 KB | None | 0 0
  1. #!/usr/bin/python2.7
  2. # -*- coding: utf-8 -*-
  3. from time import time,sleep
  4. import psutil
  5. from operator import sub
  6. from urllib2 import urlopen
  7. import threading
  8.  
  9. # For example /var/www/statistic/
  10. statStorage = ''
  11. # For example http://example.com/nginx_status
  12. # For detail, see http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
  13. stubStatusURL = ''
  14.  
  15. # Memory usage: free,proc,cache,buff
  16. def memory_usage():
  17.     meminfo = {}
  18.     timestamp = int(time())
  19.     fileName = statStorage + 'memory.csv'
  20.     file_ = open(fileName,'a')
  21.     for string in open('/proc/meminfo','r').read().split('\n')[:-1]:
  22.         string = string.split()
  23.         meminfo[string[0].strip(':')] = int(string[1])
  24.  
  25.     file_.write('%d,%d,%d,%d,%d\n' % (timestamp,
  26.                                       meminfo['MemFree'],
  27.                                       meminfo['MemTotal'] - meminfo['MemFree'] - meminfo['Cached'] - meminfo['Buffers'],
  28.                                       meminfo['Cached'],
  29.                                       meminfo['Buffers']))
  30.  
  31.  
  32. # Count of process by groups: nginx,php,mysql,standard,other
  33. def proc_count_by_groups():
  34.     standardProcNames = ['sshd', 'udevd', 'logger', 'init', 'getty', 'cron']
  35.     timestamp = int(time())
  36.     fileName = statStorage + 'proc_by_groups.csv'
  37.     file_ = open(fileName,'a')
  38.  
  39.     processes = psutil.get_process_list()
  40.     processesCount = {'nginx':0, 'php':0, 'mysql':0, 'standard':0, 'other':0}
  41.     for process in processes:
  42.         if 'nginx' in process.name: processesCount['nginx'] += 1
  43.         elif 'php' in process.name: processesCount['php'] += 1
  44.         elif 'mysql' in process.name: processesCount['mysql'] += 1
  45.         elif 'logger' in process.name: processesCount['mysql'] += 1
  46.         elif process.name in standardProcNames: processesCount['standard'] += 1
  47.         else: processesCount['other'] += 1
  48.  
  49.     file_.write('%d,%d,%d,%d,%d,%d\n' % (timestamp,
  50.                                          processesCount['nginx'],
  51.                                          processesCount['php'],
  52.                                          processesCount['mysql'],
  53.                                          processesCount['standard'],
  54.                                          processesCount['other']))
  55.  
  56. # Count of process by status: run,idle,sleep...
  57. def proc_count_by_status():
  58.     timestamp = int(time())
  59.     fileName = statStorage + 'proc_by_status.csv'
  60.     file_ = open(fileName,'a')
  61.     processes = psutil.get_process_list()
  62.     processesCount = {}
  63.     for process in processes:
  64.         processesCount[process.status] = processesCount.get(process.status, 0) + 1
  65.  
  66.  
  67.     file_.write('%d,%d,%d,%d,%d,%d,%d,%d\n' % (timestamp,
  68.                              processesCount.get(psutil.STATUS_DEAD,0),
  69.                              processesCount.get(psutil.STATUS_DISK_SLEEP,0),
  70.                              processesCount.get(psutil.STATUS_RUNNING,0),
  71.                              processesCount.get(psutil.STATUS_SLEEPING,0),
  72.                              processesCount.get(psutil.STATUS_STOPPED,0),
  73.                              processesCount.get(psutil.STATUS_TRACING_STOP,0),
  74.                              processesCount.get(psutil.STATUS_ZOMBIE,0)))
  75.  
  76. def load_average():
  77.     timestamp = int(time())
  78.     fileName = statStorage + 'load_average.csv'
  79.     file_ = open(fileName,'a')
  80.  
  81.     loadAverage = open('/proc/loadavg','r').read().split(' ',3)
  82.     file_.write('%d,%s,%s,%s\n' % (timestamp,
  83.                                    loadAverage[0],
  84.                                    loadAverage[1],
  85.                                    loadAverage[2]))
  86.  
  87. def get_context_switches():
  88.     stat = open('/proc/stat','r').read().split('\n')
  89.     for line in stat:
  90.         if 'ctxt' in line: return int(line.split(' ')[1])
  91.  
  92. def context_switches():
  93.     firstContextSwitchesCount = get_context_switches()
  94.     sleep(1)
  95.     secondContextSwitchesCount = get_context_switches()
  96.  
  97.     timestamp = int(time())
  98.     fileName = statStorage + 'context_switches.csv'
  99.     file_ = open(fileName,'a')
  100.     file_.write('%d,%d\n' % (timestamp,
  101.                              secondContextSwitchesCount - firstContextSwitchesCount))
  102.  
  103.  
  104. def get_cpu_usage():
  105.     stat = open('/proc/stat','r').read().split('\n')
  106.     for line in stat:
  107.         if 'cpu ' in line: return map(lambda x: int(x) , line.split()[1:])
  108.  
  109. def cpu_usage():
  110.     firstCpuUsage = get_cpu_usage()
  111.     sleep(1)
  112.     secondCpuUsage = get_cpu_usage()
  113.  
  114.     timestamp = int(time())
  115.     # second_cpu_usage and first_cpu_usage diff
  116.     curCpuUsage = map(sub, secondCpuUsage, firstCpuUsage)
  117.  
  118.     csvStringFormat = '%d' + ',%d' * len(curCpuUsage) + '\n'
  119.     csvValuesList = tuple([timestamp] + curCpuUsage)
  120.  
  121.     fileName = statStorage + 'cpu_usage.csv'
  122.     file_ = open(fileName,'a')
  123.     file_.write(csvStringFormat % csvValuesList)
  124.  
  125. # Need nginx module ngx_http_stub_status_module.
  126. # See http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
  127. def nginx_connections():
  128.     stubStatus = urlopen(stubStatusURL).read().split('\n')[3].split()
  129.     readingConnections = int(stubStatus[1])
  130.     writingConnections = int(stubStatus[3])
  131.     waitingConnections = int(stubStatus[5])
  132.  
  133.     timestamp = int(time())
  134.  
  135.     fileName = statStorage + 'nginx_connections.csv'
  136.     file_ = open(fileName,'a')
  137.     file_.write('%d,%d,%d,%d\n' % (timestamp,
  138.                            readingConnections,
  139.                            writingConnections,
  140.                            waitingConnections))
  141.  
  142.  
  143. # Need nginx module ngx_http_stub_status_module.
  144. # See http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
  145. def get_nginx_requests():
  146.     return int(urlopen(stubStatusURL).read().split('\n')[2].split()[2])
  147.  
  148. def nginx_rps():
  149.     firstRPS = get_nginx_requests()
  150.     sleep(1)
  151.     secondRPS = get_nginx_requests()
  152.  
  153.     timestamp = int(time())
  154.  
  155.     fileName = statStorage + 'nginx_rps.csv'
  156.     file_ = open(fileName,'a')
  157.     file_.write('%d,%d\n' % (timestamp,
  158.                              secondRPS - firstRPS))
  159.  
  160.  
  161. thread_context_switches = threading.Thread(target=context_switches)
  162. thread_cpu_usage = threading.Thread(target=cpu_usage)
  163. thread_nginx_rps = threading.Thread(target=nginx_rps)
  164.  
  165. thread_context_switches.start()
  166. thread_cpu_usage.start()
  167. thread_nginx_rps.start()
  168.  
  169. thread_context_switches.join()
  170. thread_cpu_usage.join()
  171. thread_nginx_rps.join()
  172.  
  173.  
  174. memory_usage()
  175. proc_count_by_groups()
  176. proc_count_by_status()
  177. load_average()
  178. nginx_connections()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement