Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2.7
- # -*- coding: utf-8 -*-
- from time import time,sleep
- import psutil
- from operator import sub
- from urllib2 import urlopen
- import threading
- # For example /var/www/statistic/
- statStorage = ''
- # For example http://example.com/nginx_status
- # For detail, see http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
- stubStatusURL = ''
- # Memory usage: free,proc,cache,buff
- def memory_usage():
- meminfo = {}
- timestamp = int(time())
- fileName = statStorage + 'memory.csv'
- file_ = open(fileName,'a')
- for string in open('/proc/meminfo','r').read().split('\n')[:-1]:
- string = string.split()
- meminfo[string[0].strip(':')] = int(string[1])
- file_.write('%d,%d,%d,%d,%d\n' % (timestamp,
- meminfo['MemFree'],
- meminfo['MemTotal'] - meminfo['MemFree'] - meminfo['Cached'] - meminfo['Buffers'],
- meminfo['Cached'],
- meminfo['Buffers']))
- # Count of process by groups: nginx,php,mysql,standard,other
- def proc_count_by_groups():
- standardProcNames = ['sshd', 'udevd', 'logger', 'init', 'getty', 'cron']
- timestamp = int(time())
- fileName = statStorage + 'proc_by_groups.csv'
- file_ = open(fileName,'a')
- processes = psutil.get_process_list()
- processesCount = {'nginx':0, 'php':0, 'mysql':0, 'standard':0, 'other':0}
- for process in processes:
- if 'nginx' in process.name: processesCount['nginx'] += 1
- elif 'php' in process.name: processesCount['php'] += 1
- elif 'mysql' in process.name: processesCount['mysql'] += 1
- elif 'logger' in process.name: processesCount['mysql'] += 1
- elif process.name in standardProcNames: processesCount['standard'] += 1
- else: processesCount['other'] += 1
- file_.write('%d,%d,%d,%d,%d,%d\n' % (timestamp,
- processesCount['nginx'],
- processesCount['php'],
- processesCount['mysql'],
- processesCount['standard'],
- processesCount['other']))
- # Count of process by status: run,idle,sleep...
- def proc_count_by_status():
- timestamp = int(time())
- fileName = statStorage + 'proc_by_status.csv'
- file_ = open(fileName,'a')
- processes = psutil.get_process_list()
- processesCount = {}
- for process in processes:
- processesCount[process.status] = processesCount.get(process.status, 0) + 1
- file_.write('%d,%d,%d,%d,%d,%d,%d,%d\n' % (timestamp,
- processesCount.get(psutil.STATUS_DEAD,0),
- processesCount.get(psutil.STATUS_DISK_SLEEP,0),
- processesCount.get(psutil.STATUS_RUNNING,0),
- processesCount.get(psutil.STATUS_SLEEPING,0),
- processesCount.get(psutil.STATUS_STOPPED,0),
- processesCount.get(psutil.STATUS_TRACING_STOP,0),
- processesCount.get(psutil.STATUS_ZOMBIE,0)))
- def load_average():
- timestamp = int(time())
- fileName = statStorage + 'load_average.csv'
- file_ = open(fileName,'a')
- loadAverage = open('/proc/loadavg','r').read().split(' ',3)
- file_.write('%d,%s,%s,%s\n' % (timestamp,
- loadAverage[0],
- loadAverage[1],
- loadAverage[2]))
- def get_context_switches():
- stat = open('/proc/stat','r').read().split('\n')
- for line in stat:
- if 'ctxt' in line: return int(line.split(' ')[1])
- def context_switches():
- firstContextSwitchesCount = get_context_switches()
- sleep(1)
- secondContextSwitchesCount = get_context_switches()
- timestamp = int(time())
- fileName = statStorage + 'context_switches.csv'
- file_ = open(fileName,'a')
- file_.write('%d,%d\n' % (timestamp,
- secondContextSwitchesCount - firstContextSwitchesCount))
- def get_cpu_usage():
- stat = open('/proc/stat','r').read().split('\n')
- for line in stat:
- if 'cpu ' in line: return map(lambda x: int(x) , line.split()[1:])
- def cpu_usage():
- firstCpuUsage = get_cpu_usage()
- sleep(1)
- secondCpuUsage = get_cpu_usage()
- timestamp = int(time())
- # second_cpu_usage and first_cpu_usage diff
- curCpuUsage = map(sub, secondCpuUsage, firstCpuUsage)
- csvStringFormat = '%d' + ',%d' * len(curCpuUsage) + '\n'
- csvValuesList = tuple([timestamp] + curCpuUsage)
- fileName = statStorage + 'cpu_usage.csv'
- file_ = open(fileName,'a')
- file_.write(csvStringFormat % csvValuesList)
- # Need nginx module ngx_http_stub_status_module.
- # See http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
- def nginx_connections():
- stubStatus = urlopen(stubStatusURL).read().split('\n')[3].split()
- readingConnections = int(stubStatus[1])
- writingConnections = int(stubStatus[3])
- waitingConnections = int(stubStatus[5])
- timestamp = int(time())
- fileName = statStorage + 'nginx_connections.csv'
- file_ = open(fileName,'a')
- file_.write('%d,%d,%d,%d\n' % (timestamp,
- readingConnections,
- writingConnections,
- waitingConnections))
- # Need nginx module ngx_http_stub_status_module.
- # See http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
- def get_nginx_requests():
- return int(urlopen(stubStatusURL).read().split('\n')[2].split()[2])
- def nginx_rps():
- firstRPS = get_nginx_requests()
- sleep(1)
- secondRPS = get_nginx_requests()
- timestamp = int(time())
- fileName = statStorage + 'nginx_rps.csv'
- file_ = open(fileName,'a')
- file_.write('%d,%d\n' % (timestamp,
- secondRPS - firstRPS))
- thread_context_switches = threading.Thread(target=context_switches)
- thread_cpu_usage = threading.Thread(target=cpu_usage)
- thread_nginx_rps = threading.Thread(target=nginx_rps)
- thread_context_switches.start()
- thread_cpu_usage.start()
- thread_nginx_rps.start()
- thread_context_switches.join()
- thread_cpu_usage.join()
- thread_nginx_rps.join()
- memory_usage()
- proc_count_by_groups()
- proc_count_by_status()
- load_average()
- nginx_connections()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement