Fix: filesystem report
authorArjen Baart <arjen@andromeda.nl>
Wed, 7 Oct 2020 15:55:56 +0000 (17:55 +0200)
committerArjen Baart <arjen@andromeda.nl>
Wed, 7 Oct 2020 15:55:56 +0000 (17:55 +0200)
src/gcm_daemon/gcm_daemon.php
src/gcm_input/df_filter.cpp
src/gcm_input/gcm_input.cpp
test/Makefile.am
test/df-k.out [new file with mode: 0644]
test/filesystem_report [new file with mode: 0755]

index a9da99b..38e0d3a 100755 (executable)
@@ -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'");
+            }
          }
       }
    }
index b0da126..0637bec 100644 (file)
@@ -33,8 +33,6 @@
 
 *****************************/
 
-/* static const char *RCSID = "$Id: df_filter.cpp,v 1.1 2007-11-03 10:26:13 arjen Exp $"; */
-
 #include <ctype.h>
 #include <vector>
 
@@ -220,6 +218,7 @@ void df_filter::construct_XML(message_buffer &in, std::strstream &xml)
    xml << "    </gcmt:parameters>\n";
    xml << "  </gcmt:data>\n";
    xml << "</gcmt:message>\n";
+
 }
 
 bool df_cooker::check_pattern(String logline)
index a4b8937..f9e30d9 100644 (file)
@@ -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";
index 2c5d486..394c198 100644 (file)
@@ -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 (file)
index 0000000..bace666
--- /dev/null
@@ -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 (executable)
index 0000000..44f7f1b
--- /dev/null
@@ -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' <df-k.out
+   result=$?
+   if [[ $result -eq 0 ]]
+   then
+      NR_PARS=`psql gnucomo_test -q -t -c "select count(*) from parameter"`
+      echo $NR_PARS parameters in the datebase
+      NR_PROP=`psql gnucomo_test -q -t -c "select count(*) from property"`
+      echo $NR_PROP parameter properties in the datebase
+      if [[ $NR_PARS -ne 7 ]]
+      then
+         result=2
+      fi
+      if [[ $NR_PROP -ne 28 ]]
+      then
+         result=3
+      fi
+   fi
+
+   psql gnucomo_test -f ../src/database/destroy.sql -q
+else
+   echo Can not create test database
+   result=1
+fi
+
+dropdb gnucomo_test
+exit $result