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
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# * Copyright (C) 2013 Adiscon GmbH.
# * This file is part of RSyslog
# *
# * This script processes csv stats logfiles created by statslog-splitter.py and creates graphs
# * Dependecies: - python pip -> Needed to install python packages
# * - python cairosvg -> Needed for PNG converting support!
# * - Install python packages using this command:
# * pip install CairoSVG tinycss cssselect pygal
# *
import sys
import datetime
import time
import os
import pygal
# Set default variables
szInput = ""
szOutputFile = ""
bHelpOutput = False
nMaxDataCount = 25
bUseDateTime = True
bLineChart = True
bBarChart = False
bConvertPng = False
# Init variables
aFields = []
aData = {}
aMajorXData = []
# Helper variables
nDataRecordCound = 0
nLineCount = 0
iStartSeconds = 0
# Process Arguments
for arg in sys.argv[-4:]:
if arg.find("--input=") != -1:
szInput = arg[8:]
elif arg.find("--outputdir=") != -1:
szOutputFile = arg[12:]
elif arg.find("--maxdataxlabel=") != -1:
nMaxDataCount = int(arg[16:])
elif arg.find("--xlabeldatetime") != -1:
bUseDateTime = True
elif arg.find("--xlabelseconds") != -1:
bUseDateTime = False
elif arg.find("--convertpng") != -1:
bConvertPng = True
elif arg.find("--linechart") != -1:
bLineChart = True
bBarChart = False
elif arg.find("--barchart") != -1:
bLineChart = False
bBarChart = True
elif arg.find("--h") != -1 or arg.find("-h") != -1 or arg.find("--help") != -1:
bHelpOutput = True
if bHelpOutput == True:
print "\n\nStatslog-graph command line options:"
print "======================================="
print " --input=<filename> Contains the path and filename of your impstats logfile. "
print " Default is 'rsyslog-stats.log' \n"
print " --outputfile=<dir> Output directory and file to be used. "
print " Default is '" + szOutputFile + "'. "
print " --maxdataxlabel=<num> Max Number of data shown on the x-label."
print " Default is 25 label entries."
print " --xlabeldatetime Use Readable Datetime for x label data. (Cannot be used with --xlabelseconds)"
print " Default is enabled."
print " --xlabelseconds Use seconds instead of datetime, starting at 0. (Cannot be used with --xlabeldatetime)"
print " Default is disabled."
print " --linechart Generates a Linechart (Default chart mode) (Cannot be used with --barchart)"
print " --barchart Generates a Barchart (Cannot be used with --linechart)"
print " --convertpng Generate PNG Output rather than SVG. "
print " Default is SVG output."
print " --h / -h / --help Displays this help message. \n"
print "\n Sampleline: ./statslog-graph.py --input=imuxsock.csv --outputfile=/home/user/csvgraphs/imuxsock.svg"
else:
# Generate output filename
if len(szInput) > 0:
if szInput.rfind(".") == -1:
szOutputFile += szInput + ".svg"
else:
szOutputFile += szInput[:-4] + ".svg"
else:
print "Error, no input file specified!"
sys.exit(0)
# Process inputfile
inputfile = open(szInput, 'r')
for line in inputfile.readlines():
if nLineCount == 0:
aFields = line.strip().split(";")
# remove last item if empty
if len(aFields[len(aFields)-1]) == 0:
aFields.pop()
#print aFields
#sys.exit(0)
#Init data arrays
for field in aFields:
aData[field] = []
else:
aLineData = line.strip().split(";")
# remove last item if empty
if len(aLineData[len(aLineData)-1]) == 0:
aLineData.pop()
# Loop Through line data
iFieldNum = 0
for field in aFields:
if iFieldNum == 0:
if bUseDateTime:
aData[field].append( datetime.datetime.strptime(aLineData[iFieldNum],"%Y/%b/%d %H:%M:%S") )
else:
# Convert Time String into UNIX Timestamp
myDateTime = datetime.datetime.strptime(aLineData[iFieldNum],"%Y/%b/%d %H:%M:%S")
iTimeStamp = int(time.mktime(myDateTime.timetuple()))
# Init Start Seconds
if iStartSeconds == 0:
iStartSeconds = iTimeStamp
# Set data field
aData[field].append( iTimeStamp - iStartSeconds )
elif iFieldNum > 2:
aData[field].append( int(aLineData[iFieldNum]) )
else:
aData[field].append( aLineData[iFieldNum] )
iFieldNum += 1
# print aData[field[nLineCount]]
# Increment counter
nDataRecordCound += 1
#print aData
#sys.exit(0)
# Increment counter
nLineCount += 1
# if nLineCount > 25:
# break
if nMaxDataCount > 0:
# Check if we need to reduce the data amount
nTotalDataCount = len( aData[aFields[0]] )
nDataStepCount = nTotalDataCount / (nMaxDataCount)
if nTotalDataCount > nMaxDataCount:
for iDataNum in reversed(range(0, nTotalDataCount)):
# Remove all entries who
if iDataNum % nDataStepCount == 0:
aMajorXData.append( aData[aFields[0]][iDataNum] )
# for field in aFields:
# aData[field].pop(iDataNum)
# print len(aMajorXData)
# sys.exit(0)
# Create Config object
from pygal import Config
chartCfg = Config()
chartCfg.show_legend = True
chartCfg.human_readable = True
chartCfg.pretty_print=True
chartCfg.fill = False
chartCfg.x_scale = 1
chartCfg.y_scale = 1
chartCfg.x_label_rotation = 45
chartCfg.include_x_axis = True
chartCfg.show_dots=False
chartCfg.show_minor_x_labels=False
#chartCfg.logarithmic=True # Makes chart more readable
#Linechart
if bLineChart:
myChart = pygal.Line(chartCfg)
myChart.title = 'Line Chart of "' + szInput + '"'
myChart.x_title = "Time elasped in seconds"
myChart.x_labels = map(str, aData[aFields[0]] )
myChart.x_labels_major = map(str, aMajorXData )
for iChartNum in range(3, len(aFields) ):
myChart.add(aFields[iChartNum], aData[ aFields[iChartNum] ]) # Add some values
elif bBarChart:
myChart = pygal.Bar(chartCfg)
myChart.title = 'Bar Chart of "' + szInput + '"'
myChart.x_title = "Time elasped in seconds"
myChart.x_labels = map(str, aData[aFields[0]] )
myChart.x_labels_major = map(str, aMajorXData )
for iChartNum in range(3, len(aFields) ):
myChart.add(aFields[iChartNum], aData[ aFields[iChartNum] ]) # Add some values
# Render Chart now and output to file!
myChart.render_to_file(szOutputFile)
# Convert to PNG and remove SVG
if bConvertPng:
szPngFileName = szOutputFile[:-4] + ".png"
iReturn = os.system("cairosvg " + szOutputFile + " -f png -o " + szPngFileName)
print "File SVG converted to PNG: '" + szPngFileName + "', return value of cairosvg: " + str(iReturn)
os.remove(szOutputFile)
# Finished
sys.exit(0)
|