Skip to content

Instantly share code, notes, and snippets.

@Oreolek
Created May 12, 2017 06:24
Show Gist options
  • Save Oreolek/3d86ae3ce7381c7bb2846b009c49c797 to your computer and use it in GitHub Desktop.
Save Oreolek/3d86ae3ce7381c7bb2846b009c49c797 to your computer and use it in GitHub Desktop.
log parser
Feb 10 19:02:17 mail sm-mta[31511]: v1AI2GTr031509: to=<Dalmer.Dalmer@mail.tester-club.com>, ctladdr=<www-data@mail.tester-club.com> (33/33), delay=00:00:01, xdelay=00:00:00, mailer=relay, pri=169116, relay=mxlb.ispgateway.de. [80.67.18.126], dsn=5.0.0, stat=Service unavailable
<?php
define('INPUT', 'input.txt');
$servername = "localhost";
$username = "username";
$password = "password";
$emails = [];
if ($file = fopen(INPUT, "r")) {
if (!$file)
die('file does not exist or cannot be opened');
while (($line = fgets($file)) !== false) {
$date = new DateTime(substr($line, 0, 16));
$line = substr($line, 52);
$vars = explode(', ', $line);
$options = [];
foreach ($vars as $value) {
$temp = explode('=', $value);
$options[$temp[0]] = trim($temp[1]);
}
unset($vars);
if (!isset($emails[$options['stat']])) {
$emails[$options['stat']] = [];
}
$emails[$options['stat']][] = $options['to'];
}
fclose($file);
}
$errors = array_keys($emails);
sort($errors, SORT_STRING);
print_r($errors);
print_r($emails);
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($emails as $error => $list) {
$sql = "UPDATE connections SET count='".count($list)."' WHERE error='".$error."'";
$conn->query($sql);
}
$conn->close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment