09 May

error logger, mantisated

The plugin architecture for Mantis is not yet mature, so I’ve bypassed it in this. Instead, this script will inject issues directly into the Mantis database.

The following script will load up all errors aggregated with the last post’s script, and will add them as Mantis issues (or update existing issues if found).

<?php
$t=time();
$project_id=6;
$error_urls=array(
  'http://the.url/error_checker/?password=abcdefg&maxlines=5000&last_date_read='.($t-3600)
);

require 'config_inc.php';
$db=new PDO($g_db_type.':host='.$g_hostname.';dbname='.$g_database_name,$g_db_username,$g_db_password);
$db->query('SET NAMES utf8');

foreach($error_urls as $eu){
  echo $eu."\n";
  $errors=json_decode(file_get_contents($eu));
  foreach($errors as $err){
    $md5=addslashes($err->md5);
    $q=$db->query("select id,summary from mantis_bug_table where summary like '$md5 %' limit 1");
    $r=$q->fetch(PDO::FETCH_ASSOC);
    $cnt=(int)$err->count;
    if($r){
      $id=$r['id'];
      $cnt=preg_replace('/.* \(([0-9]*)\)/','\1',$r['summary'])+$cnt;
      $db->query("update mantis_bug_table set status=10,summary='$md5 ($cnt)' where id=$id");
      $btext=addslashes($err->log);
      $db->query("insert into mantis_bugnote_text_table values(0,'$btext')");
      $btext_id=($db->query('select last_insert_id() as id')->fetch());
      $btext_id=(int)$btext_id['id'];
      $db->query("insert into mantis_bugnote_table set bug_id=$id,bugnote_text_id=$btext_id,date_submitted=now(),last_modified=now()");
    }
    else{
      $db->query("insert into mantis_bug_table set date_submitted=now(),summary='$md5 ($cnt)',project_id=$project_id");
      $id=($db->query('select last_insert_id() as id')->fetch());
      $id=(int)$id['id'];
      $db->query("insert into mantis_bug_text_table set id=$id,description='".addslashes($err->error)."',additional_information='".addslashes($err->log)."'");
      $btext_id=($db->query('select last_insert_id() as id')->fetch());
      $btext_id=(int)$btext_id['id'];
      $db->query("update mantis_bug_table set bug_text_id=$btext_id where id=$id");
    }
    $db->query("update mantis_bug_table set last_updated=now() where id=$id");
  }
}

Note the bold lines – the first indicates what Mantis project to place the issue under. In my case, I created a new project named “httpd errors”, which had the ID 6. The second is an array of URLs pointing to where the error aggregators can be found. In this case, the Mantis injector is going to be called from cron every hour, so the URL includes a time limit looking for errors only in the previous hour (3600 seconds).

2 thoughts on “error logger, mantisated

  1. Hi Kae,

    Since I created the plugin system, and I’ve been constantly trying to improve it to fit whatever needs may arise from users and developers, what suggestions or complaints can you lend in regards to the “not yet mature” topic? What could be improved or implemented? Thanks for any feedback you can offer me.

    Cheers

  2. hi John. The first and /most important/ thing I can mention is that documentation is king.

    when I started writing this, I had hoped to figure a way of doing it using either a plugin, or an API. I couldn’t find any information about an API, and it turns out a plugin would be inappropriate for this, as it’s a timed thing – it should really be called automatically once every n minutes.

    “not yet mature” is not quite the best description – I haven’t been able to find a solid definition yet for Mantis plugins yet, so maybe that was a hasty statement to make.

    Perhaps more important to me would be an API, where I can call Mantis functions or tell Mantis to perform some actions, without needing to actually hook into the source.

Leave a Reply