#!/usr/bin/python """Copyright 2008 Orbitz WorldWide Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.""" import sys import time import os import platform import subprocess from socket import socket import httplib2 CARBON_SERVER = '127.0.0.1' CARBON_PORT = 2003 delay = 10 if len(sys.argv) > 1: delay = int( sys.argv[1] ) def time_url(url): fetcher = httplib2.Http(".cache") t1=time.time() resp, content = fetcher.request(url,headers={'cache-control':'no-cache'}) duration = (time.time() - t1) resp_code = int(resp["status"]) if 200 <= resp_code < 400: return duration return -1 def get_timeurl(): return time_url('http://192.168.100.209/mysite/') def get_loadavg(): # For more details, "man proc" and "man uptime" if platform.system() == "Linux": return open('/proc/loadavg').read().strip().split()[:3] else: command = "uptime" process = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True) os.waitpid(process.pid, 0) output = process.stdout.read().replace(',', ' ').strip().split() length = len(output) return output[length - 3:length] sock = socket() try: sock.connect( (CARBON_SERVER,CARBON_PORT) ) except: print "Couldn't connect to %(server)s on port %(port)d, is carbon-agent.py running?" % { 'server':CARBON_SERVER, 'port':CARBON_PORT } sys.exit(1) while True: now = int( time.time() ) lines = [] #We're gonna report all three loadavg values timeurl = get_timeurl() lines.append("mysite.web_responsetime %s %d" % (timeurl,now)) message = '\n'.join(lines) + '\n' #all lines must end in a newline print "sending message\n" print '-' * 80 print message print sock.sendall(message) time.sleep(delay)