Skip to content

Commit

Permalink
added graph.ini, consolidate graph settings into .ini
Browse files Browse the repository at this point in the history
removing the need to edit graph.js to alter the DB file/path, or listening IP

will use old .ini files if newer config.ini not present, and retains defaults
  • Loading branch information
msimerson committed Dec 23, 2013
1 parent 0117e97 commit 7d71e01
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
12 changes: 12 additions & 0 deletions config/graph.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; the filename the SQLite database is stored in
; can be a file name or :memory:
;db_file=:memory:
db_file=graphlog.db

; The port to listen on for http. Default: `8080`.
http_addr=127.0.0.1
http_port=8080

; Regular expression to match plugins to ignore for logging.
; Default: `queue|graph|relay`
ignore_re=`queue|graph|relay`
18 changes: 14 additions & 4 deletions docs/plugins/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ This plugin logs accepted and rejected emails into a database and provides
a web server which you can browse to and view graphs over time of the
plugins which rejected connections.

In order for this to work you need to install the `sqlite` module via
`npm install sqlite` in your Haraka directory.
In order for this to work you need to install the `sqlite3` module via
`npm install sqlite3` in your Haraka directory.

Configuration
-------------

* grapher.http_port
config settings are stored in config/graph.ini

* db_file

The file name (or :memory:), where data is stored

* http_addr

The IP address to listen on for http. Default: `127.0.0.1`.

* http_port

The port to listen on for http. Default: `8080`.

* grapher.ignore_re
* ignore_re

Regular expression to match plugins to ignore for logging.
Default: `queue|graph|relay`
35 changes: 20 additions & 15 deletions plugins/graph.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// log our denys

var http = require('http');
var urlp = require('url');
var utils = require('./utils');

var sqlite3 = require('sqlite3').verbose();
// var db = new sqlite3.Database(':memory:', createTable);
var db = new sqlite3.Database('graphlog.db', createTable);

var db;
var select = "SELECT COUNT(*) AS hits, plugin FROM graphdata WHERE timestamp >= ? AND timestamp < ? GROUP BY plugin";
var insert = db.prepare( "INSERT INTO graphdata VALUES (?,?)" );
var insert;
var plugins = {};
var config;

var width = 800;

function createTable() {
db.exec( "CREATE TABLE IF NOT EXISTS graphdata (timestamp INTEGER NOT NULL, plugin TEXT NOT NULL)")
.exec( "CREATE INDEX IF NOT EXISTS graphdata_idx ON graphdata (timestamp)");
}

var plugins = {};

var http = require('http');
var urlp = require('url');
var utils = require('./utils');
var width = 800;

exports.register = function () {
var plugin = this;
var ignore_re = this.config.get('grapher.ignore_re') || 'queue|graph|relay';
config = this.config.get('graph.ini');
var ignore_re = config.main.ignore_re || this.config.get('grapher.ignore_re') || 'queue|graph|relay';
ignore_re = new RegExp(ignore_re);

plugins = {accepted: 0, disconnect_early: 0};
Expand All @@ -33,11 +33,16 @@ exports.register = function () {
}
}
);

var db_name = config.main.db_file || 'graphlog.db';
db = new sqlite3.Database(db_name, createTable);
insert = db.prepare( "INSERT INTO graphdata VALUES (?,?)" );
};

exports.hook_init_master = function (next) {
var plugin = this;
var port = this.config.get('grapher.http_port') || 8080;
var port = config.main.http_port || this.config.get('grapher.http_port') || 8080;
var addr = config.main.http_addr || '127.0.0.1';
var server = http.createServer(
function (req, res) {
plugin.handle_http_request(req, res);
Expand All @@ -48,8 +53,8 @@ exports.hook_init_master = function (next) {
next(DENY);
});

server.listen(port, "127.0.0.1", function () {
plugin.loginfo("http server running on port " + port);
server.listen(port, addr, function () {
plugin.loginfo("http server running on " + addr + ':' port);
next();
});
}
Expand Down

0 comments on commit 7d71e01

Please sign in to comment.