/usr/lib/tsung/tsung_plotter/tsung.py is in tsung 1.4.2-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | #! /usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: 2006 by Dalibo <dalibo.com>
# Copyright: 2007 Dimitri Fontaine <dim@tapoueh.org>
# Created: 2006 by Dimitri Fontaine <dim@tapoueh.org>
#
# Modified: 2008 by Nicolas Niclausse
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
# In addition, as a special exception, you have the permission to
# link the code of this program with any library released under
# the EPL license and distribute linked combinations including
# the two.
"""
Python classes for tsung log data usage
data: generic tsung data
sample
counter
gauge
log: tsung log file parser, using sample, gauge and counter classes
This package has its own configuration file where to associate tsung
stats names with tsung stat type.
"""
from ConfigParser import ConfigParser
# data are produced every 10 seconds, by default
# we read real used interval in log file (deduced from timestamps)
INTERVAL = 10
SEPARATOR = "# stats: dump at "
PREFIX = "stats: "
class data:
""" Tsung logged data, to be specialized into sample and counter """
def __init__(self):
pass
def get(self, name):
""" get named statistic """
if name not in self.__dict__.keys():
return None
# don't return any value when no measure have been taken
# durung interval
if self.count == 0:
return None
if name == 'count' :
if self.interval > 0:
return self.count / self.interval
else:
return 0
return self.__dict__[name]
class sample(data):
""" tsung sample stats """
def __init__(self, interval = INTERVAL, values = [0, 0, 0, 0, 0, 0]):
""" init log data values """
self.interval = interval
self.count = float(values[0])
self.mean = float(values[1])
self.stdvar = float(values[2])
self.min = float(values[3])
self.max = float(values[4])
self.gmean = float(values[5])
def __repr__(self):
""" human readable output """
return "sample: %s %s %s %s %s" % (self.value, self.mean,
self.stdvar, self.min, self.max, self.gmean)
class counter(data):
""" tsung counter stats """
def __init__(self, interval = INTERVAL, values = [0, 0]):
""" init log data values """
self.interval = 1
self.count = float(values[0])
self.totalcount = float(values[1])
def __repr__(self):
""" human readable output """
return "counter: %s %s" % (self.value, self.totalcount)
class gauge(data):
""" tsung gauge stats """
def __init__(self, interval = INTERVAL, values = [0, 0]):
""" init log data values """
self.interval = 1
self.count = float(values[0])
self.max = float(values[1])
def __repr__(self):
""" human readable output """
return "gauge: %s %s" % (self.count, self.max)
class TsungLog:
""" Tsung logged data parser and representation
data is a {timestamp, data} hash, with data either sample or
counter instance.
"""
def __init__(self, config_filename, filename):
""" constructor """
self.conffile = config_filename
self.filename = filename
self.types = {}
self.data = {}
self.unknown = []
self.configure()
self.parse()
def configure(self):
""" read configuration file to associates stat types to stat names """
config = ConfigParser()
config.read(self.conffile)
for s in config.sections():
for (name, type) in config.items(s):
if type == 'sample':
self.types[name] = sample()
if type == 'counter':
self.types[name] = counter()
if type == 'gauge':
self.types[name] = gauge()
def parse(self):
""" read data from self.filename """
first_ts = 0
current_ts = 0
record_ts = 0
import re
for line in file(self.filename):
# chomp \n
line = line[:-1]
if line.find(SEPARATOR) == 0:
measure_ts = int(line[len(SEPARATOR):].strip())
# interval in seconds between two records. As tsung
# generates a reference line, sensible values won't
# have to divide by 0
interval = measure_ts - current_ts
# now we change current_ts
current_ts = measure_ts
if first_ts == 0:
first_ts = current_ts
record_ts = current_ts - first_ts
elif line.find("stats: ") == 0:
array = [v.strip() for v in line.split(" ")]
name = array[1]
values = array[2:]
if self.types.has_key(name):
data = self.types[name].__class__(interval, values)
if not self.data.has_key(record_ts):
self.data[record_ts] = {}
self.data[record_ts][name] = data
else:
is_re = False
x = re.compile("^[\w\d]+$")
for k in self.types.keys():
if not re.match(x, k):
y = re.compile(k)
if re.match(y, name):
data = self.types[k].__class__(interval, values)
if not self.data.has_key(record_ts):
self.data[record_ts] = {}
self.data[record_ts][name] = data
is_re = True
break
if name not in self.unknown and not is_re:
print 'WARNING: tsung %s data is not configured' % name
self.unknown.append(name)
def stat(self, name, stat):
""" returns a {timestamp: date_type} dict for given named statistic """
ret = {}
for ts, stats in self.data.items():
if stats.has_key(name) and stats[name].get(stat) is not None:
ret[ts] = stats[name].get(stat)
return ret
if __name__ == '__main__':
# some unit testing
import sys
config = "stats.conf"
logfile = sys.argv[1]
tsunglog = TsungLog(config, logfile)
from pprint import pprint
pprint(tsunglog.stat('request', 'mean'))
pprint(tsunglog.stat('users', 'max'))
pprint(tsunglog.stat('finish_users_count', 'count'))
pprint(tsunglog.stat('200', 'count'))
|