From 9bf283c77ea4965f4dfd7301aa956c9ef007667a Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Wed, 7 Oct 2020 17:55:56 +0200 Subject: [PATCH] Fix: filesystem report --- src/gcm_daemon/gcm_daemon.php | 63 ++++++++++++++++++++++++++----------------- src/gcm_input/df_filter.cpp | 3 +-- src/gcm_input/gcm_input.cpp | 6 +---- test/Makefile.am | 3 ++- test/df-k.out | 12 +++++++++ test/filesystem_report | 43 +++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 33 deletions(-) create mode 100644 test/df-k.out create mode 100755 test/filesystem_report diff --git a/src/gcm_daemon/gcm_daemon.php b/src/gcm_daemon/gcm_daemon.php index a9da99b..38e0d3a 100755 --- a/src/gcm_daemon/gcm_daemon.php +++ b/src/gcm_daemon/gcm_daemon.php @@ -484,7 +484,13 @@ function match_log_patterns($logstart) //echo "Recording abuse for address ", $srv_pat->argument, "\n Log entry:\n "; //echo $logentry->rawdata, "\n Pattern:\n ", $srv_pat->pattern, "\n\n"; - if (record_abuse($logentry->logid, $logentry->objectid, $srv_pat->argument, 1) >= 32) + $nr_abuses = record_abuse($logentry->logid, $logentry->objectid, $srv_pat->argument, 1); + if ($nr_abuses < 0) + { + echo "ERROR in recording abuse for address ", $srv_pat->argument, "\n Log entry:\n "; + echo $logentry->rawdata, "\n Pattern:\n ", $srv_pat->pattern, "\n\n"; + } + if ($nr_abuses >= 32) { $source_ip = gethostbyname($srv_pat->argument); $notif = 'abuses exceeded'; @@ -556,36 +562,43 @@ function record_abuse($logid, $objectid, $sourceip, $points) $abres = $dbms->query("SELECT * FROM object_abuse WHERE objectid='$objectid' AND source='$sourceip'"); - if (pg_numrows($abres) == 0 && $points > 0) + if ($abres == false) { - //echo "$sourceip is new.\n"; - $dbms->query("INSERT INTO object_abuse VALUES ('$objectid', '$sourceip', '$points', '', NOW())"); - $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')"); + $abuse_points = -1; // return an error } - else if (pg_numrows($abres) != 0) + else { - $abuse = $dbms->fetch_object($abres, 0); - if ($abuse->status == '' || $abuse->status == 'dropped') + if (pg_numrows($abres) == 0 && $points > 0) + { + //echo "$sourceip is new.\n"; + $dbms->query("INSERT INTO object_abuse VALUES ('$objectid', '$sourceip', '$points', '', NOW())"); + $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')"); + } + else if (pg_numrows($abres) != 0) { - $abuse_points = $abuse->nr_abuses + $points; - if ($abuse_points < 0) + $abuse = $dbms->fetch_object($abres, 0); + if ($abuse->status == '' || $abuse->status == 'dropped') { - $abuse_points = 0; - } - //echo $sourceip . " will get " . $abuse_points . " abuse points, "; - //echo "Status was " . $abuse->status . "\n"; - $dbms->query("UPDATE object_abuse SET nr_abuses='$abuse_points'" . - ", last_change=NOW() WHERE objectid='$objectid' AND source='$sourceip'"); + $abuse_points = $abuse->nr_abuses + $points; + if ($abuse_points < 0) + { + $abuse_points = 0; + } + //echo $sourceip . " will get " . $abuse_points . " abuse points, "; + //echo "Status was " . $abuse->status . "\n"; + $dbms->query("UPDATE object_abuse SET nr_abuses='$abuse_points'" . + ", last_change=NOW() WHERE objectid='$objectid' AND source='$sourceip'"); - if ($points > 0) - { - $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')"); - } - if ($abuse_points >= 32) - { - //echo " BLOCK IP adrress $sourceip on the firewall.\n"; - $dbms->query("UPDATE object_abuse SET status='dropped'" . - " WHERE objectid='$objectid' AND source='$sourceip'"); + if ($points > 0) + { + $dbms->query("INSERT INTO log_abuse VALUES ('$logid', '$objectid', '$sourceip')"); + } + if ($abuse_points >= 32) + { + //echo " BLOCK IP adrress $sourceip on the firewall.\n"; + $dbms->query("UPDATE object_abuse SET status='dropped'" . + " WHERE objectid='$objectid' AND source='$sourceip'"); + } } } } diff --git a/src/gcm_input/df_filter.cpp b/src/gcm_input/df_filter.cpp index b0da126..0637bec 100644 --- a/src/gcm_input/df_filter.cpp +++ b/src/gcm_input/df_filter.cpp @@ -33,8 +33,6 @@ *****************************/ -/* static const char *RCSID = "$Id: df_filter.cpp,v 1.1 2007-11-03 10:26:13 arjen Exp $"; */ - #include #include @@ -220,6 +218,7 @@ void df_filter::construct_XML(message_buffer &in, std::strstream &xml) xml << " \n"; xml << " \n"; xml << "\n"; + } bool df_cooker::check_pattern(String logline) diff --git a/src/gcm_input/gcm_input.cpp b/src/gcm_input/gcm_input.cpp index a4b8937..f9e30d9 100644 --- a/src/gcm_input/gcm_input.cpp +++ b/src/gcm_input/gcm_input.cpp @@ -251,11 +251,7 @@ int main(int argc, char *argv[]) if (log_method == "file" && log_destination != "") { -#if __GNUC__ == 2 - logfile.open(log_destination, _IO_APPEND); // for gcc 2 -#else - logfile.open(log_destination, std::ios_base::app); // for gcc 3 -#endif + logfile.open(log_destination, std::ios_base::app); if (!logfile) { std::cerr << "Can't open logfile " << log_destination << " for writing.\n"; diff --git a/test/Makefile.am b/test/Makefile.am index 2c5d486..394c198 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,5 +1,6 @@ TESTS = createdb upgradedb read_messages read_bad_messages read_apache_error read_without_hostname \ - read_no_database \ + read_no_database read_xml_log \ + filesystem_report \ notifications notification_sendmail clean-local: diff --git a/test/df-k.out b/test/df-k.out new file mode 100644 index 0000000..bace666 --- /dev/null +++ b/test/df-k.out @@ -0,0 +1,12 @@ + + + + +Filesystem 1k-blocks Used Available Use% Mounted on +/dev/hda1 497829 82287 389840 18% / +/dev/hda5 1035660 32892 950160 4% /tmp +/dev/hda3 4127108 2651528 1265932 68% /usr +/dev/hda2 8254272 3146204 4688772 41% /var +/dev/hda7 42734172 32828 40530564 1% /mnt/tmp +/dev/hdc1 158309288 61102016 89165540 41% /mnt/backup +/dev/hdd1 158309288 120933168 29334388 81% /mnt/spare diff --git a/test/filesystem_report b/test/filesystem_report new file mode 100755 index 0000000..44f7f1b --- /dev/null +++ b/test/filesystem_report @@ -0,0 +1,43 @@ +#!/bin/bash + +# +# Create an object 'test.gnucomo.org' in the database and +# and read several filesystems report created by df. + +rm -f gcm_input.log + +createdb gnucomo_test +result=1 + +if psql gnucomo_test -q <../src/database/create.sql >/dev/null +then + psql gnucomo_test -q -c "insert into object (objectname) values ('test.gnucomo.org')" + + # read the output from 'df -k' + echo "Read output from 'df -k'" + ../src/gcm_input/gcm_input -v -c gnucomo_test -h test.gnucomo.org -d 'sep 7 2007 13:20:45'