/usr/share/httpry/plugins/db_dump.pm is in httpry-tools 0.1.8-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 | #
# ----------------------------------------------------
# httpry - HTTP logging and information retrieval tool
# ----------------------------------------------------
#
# Copyright (c) 2005-2014 Jason Bittel <jason.bittel@gmail.com>
#
package db_dump;
use warnings;
use DBI;
# -----------------------------------------------------------------------------
# GLOBAL VARIABLES
# -----------------------------------------------------------------------------
my $dbh;
# -----------------------------------------------------------------------------
# Plugin core
# -----------------------------------------------------------------------------
main::register_plugin();
sub new {
return bless {};
}
sub init {
my $self = shift;
my $cfg_dir = shift;
my $sql;
my $limit;
_load_config($cfg_dir);
$dbh = _connect_db($type, $db, $host, $port, $user, $pass);
# Delete data inserted $rmbefore days prior
if ($rmbefore > 0) {
my ($year, $mon, $day, $hour, $min, $sec) = (localtime(time-(86400*$rmbefore)))[5,4,3,2,1,0];
$limit = ($year+1900) . "-" . ($mon+1) . "-$day $hour:$min:$sec";
$sql = qq{ DELETE FROM client_data WHERE timestamp < '$limit' };
_execute_query($dbh, $sql);
$sql = qq{ DELETE FROM server_data WHERE timestamp < '$limit' };
_execute_query($dbh, $sql);
}
return;
}
sub list {
return qw(direction timestamp source-ip dest-ip);
}
sub main {
my $self = shift;
my $record = shift;
my $sth;
my ($year, $mon, $day, $hour, $min, $sec) = (localtime)[5,4,3,2,1,0];
my $now = ($year+1900) . "-" . ($mon+1) . "-$day $hour:$min:$sec";
my @values = ($now, $record->{"timestamp"}, $record->{"source-ip"}, $record->{"dest-ip"});
if ($record->{"direction"} eq '>') {
push @values, $record->{"host"}, $record->{"request-uri"};
$sth = $dbh->prepare(qq{ INSERT INTO client_data (timestamp, pktstamp, src_ip, dst_ip, hostname, uri)
VALUES (?, ?, ?, ?, ?, ?) });
} elsif ($record->{"direction"} eq '<') {
push @values, $record->{"status-code"}, $record->{"reason-phrase"};
$sth = $dbh->prepare(qq{ INSERT INTO server_data (timestamp, pktstamp, src_ip, dst_ip, status_code, reason_phrase)
VALUES (?, ?, ?, ?, ?, ?) });
}
$sth->execute(@values);
return;
}
sub end {
_disconnect_db();
return;
}
# -----------------------------------------------------------------------------
# Load config file and check for required options
# -----------------------------------------------------------------------------
sub _load_config {
my $cfg_dir = shift;
# Load config file; by default in same directory as plugin
if (-e "$cfg_dir/" . __PACKAGE__ . ".cfg") {
require "$cfg_dir/" . __PACKAGE__ . ".cfg";
} else {
die "No config file found\n";
}
# Check for required options and combinations
if (!$type) {
die "No database type provided\n";
}
if (!$db) {
die "No database name provided\n";
}
if (!$host) {
die "No database hostname provided\n";
}
$port = '3306' unless ($port);
return;
}
# -----------------------------------------------------------------------------
# Build connection to specified database
# -----------------------------------------------------------------------------
sub _connect_db {
my $type = shift;
my $db = shift;
my $host = shift;
my $port = shift;
my $user = shift;
my $pass = shift;
my $dbh;
my $dsn;
$dsn = "DBI:$type:$db";
$dsn .= ":$host" if $host;
$dsn .= ":$port" if $port;
if ($dbh = DBI->connect($dsn, $user, $pass, { PrintError => 0, RaiseError => 0, AutoCommit => 1 })) {
_execute_query($dbh, qq{ USE $db });
} else {
die "Cannot connect to database: " . DBI->errstr . "\n";
}
return $dbh;
}
# -----------------------------------------------------------------------------
# Generalized SQL query execution sub
# -----------------------------------------------------------------------------
sub _execute_query {
my $dbh = shift;
my $sql = shift;
my $sth;
$sth = $dbh->prepare($sql) or die "Cannot prepare query: " . DBI->errstr . "\n";
$sth->execute() or die "Cannot execute query: " . DBI->errstr . "\n";
return $sth;
}
# -----------------------------------------------------------------------------
# Terminate active database connection
# -----------------------------------------------------------------------------
sub _disconnect_db {
$dbh->disconnect;
}
1;
|