--[[ Copyright (C) 2010 startx 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 3 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. http://www.gnu.org/copyleft/gpl.html --]] module("torwatch", package.seeall) ------ -- trim -- function trim(string) return (string.gsub(string, "^%s*(.-)%s*$", "%1")) end ------ -- round -- function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end ------ -- split -- function split(str, pat) local t = {} local fpat = "(.-)" .. pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end ------ -- -- function find_max_val(tt) local max = 0 for i, val in ipairs(tt) do if val > max then max = val end end return max end ------ -- -- function draw_graph(data, graph) graph.width = 10 + ( graph.block_width * table.getn(data) ) + 12 local maximum = find_max_val(data) -- start drawing the graph local im = assert(gd.createTrueColor(graph.width, graph.height)) local black = im:colorAllocate(0, 0, 0) local white = im:colorAllocate(255, 255, 255) local red = im:colorAllocate(0, 255, 255) im:filledRectangle(0, 0, graph.width, graph.height, white) local right = graph.block_width + 10 local left = 10 for i,v in ipairs(data) do if math.mod(i, 2) == 0 then color = im:colorAllocate(0, 0, 0) else color = im:colorAllocate(100, 100, 100) end local perc = math.floor( ( data[i] / maximum ) * 100 ) local height = graph.height - perc left = left + 2 right = right + 2 local val = data[i] / 1024 / 1024 if val >= 1 then val = math.floor(val) else val = round(val, 1) end im:filledRectangle(left, graph.height, right, height , color) im:string(gd.FONT_TINY, left , height - 10 , val , color) left = left + graph.block_width + 2 right = right + graph.block_width + 2 end -- writing the image im:png(graph.outfile) end -- end draw_graph ------ -- -- read_traffic_data -- @return dataset table function read_traffic_data() for line in io.lines(CONF.state_file) do if string.find(line,"BWHistoryReadValues") then if not data_str_read then data_str_read = trim(string.gsub(line, "BWHistoryReadValues","")) end end if string.find(line,"BWHistoryWriteValues") then if not data_str_write then data_str_write = trim(string.gsub(line, "BWHistoryWriteValues","")) end end end local dataset = {} local tmp_read = split(data_str_read, ",") dataset.tr_read = {} for i,v in ipairs(tmp_read) do table.insert(dataset.tr_read, tonumber(v)) end local tmp_write = split(data_str_write, ",") dataset.tr_write = {} for i,v in ipairs(tmp_write) do table.insert(dataset.tr_write, tonumber(v)) end return dataset end -- end read traffic data ------ -- -- @return values table function get_values() local look_for = { "AccountingIntervalStart", "AccountingBytesReadInInterval" , "AccountingBytesWrittenInInterval", "AccountingExpectedUsage", "BWHistoryReadEnds" , "TorVersion", "LastWritten"} local values = {} for line in io.lines(CONF.state_file) do for i, looked_for_value in ipairs(look_for) do if string.match(line, looked_for_value) then values[looked_for_value] = string.gsub(line, looked_for_value.." ", "") end end end return values end -- end get values ------ -- -- @return html function create_html() local values = get_values() local time_now = os.date() -- template html = [[ ]]..time_now..[[
AccountingIntervalStart: ]]..values.AccountingIntervalStart..[[ | BWHistoryReadEnds ]]..values.BWHistoryReadEnds..[[
AccountingExpectedUsage: ]]..values.AccountingExpectedUsage..[[
read (MB): ( AccountingBytesReadInInterval: ]]..values.AccountingBytesReadInInterval..[[ )
written (MB): ( AccountingBytesWrittenInInterval: ]]..values.AccountingBytesWrittenInInterval..[[ )

LastWritten: ]]..values.LastWritten..[[
TorVersion: ]]..values.TorVersion..[[
generated by torwatch ]]..CONF.torwatch_version..[[ - GPL v3 projects.plentyfact.org/wiki/torwatch
]] return html end -- end create_html read_traffic_data()