next up previous contents
Next: pppconn.sh Up: Quellcode Previous: admin.sql   Contents


cron_job.php4


<?php
  /*******************************************
  /* Programm cron_job.php4
  /* Cron-skript to download data from computer,
  /* extract it and save it into database
  /* (c) Copyright 2002, Jens Bierkandt
  /* e-mail: jens@bierkandt.org
  /* Entstanden im Rahmen meiner Diplomarbeit
  /*******************************************
  */
  // Timeout to ftp-connection is 90 sec
  error_reporting(63); // 0 = none, 63 = full
  include("/home/schtorch/public_html/diplom/config.php4");
  include("./convert2mysql.php4");
  // Get computer details from database
  $db = mysql_connect($hostname, $username, $password);
  mysql_select_db($database, $db);
  $result = mysql_query("SELECT *,
    DECODE(passwd,\"$secret_key\") FROM `admin`", $db);
  $i = 0;
  while ($row = mysql_fetch_row($result)) {
    echo mysql_error($db);
    $name[$i] = $row[0];
    $computer[$i] = $row[1];
    $table[$i] = $row[2];
    $phone[$i] = $row[3];
    $protocol[$i] = $row[4];
    $user[$i] = $row[5];
    $passwd[$i] = $row[7];
    $i++;
  }
  $fp_log =
    fopen("/home/schtorch/public_html/diplom/diplom.log",
    "a+");
  for ($i = 0; $i < sizeof($name); $i++) {
    // connect to computer
    fputs($fp_log, date('Y-m-d H:i:s')." - Anwahl von
      ".$computer[$i]." mit ".$phone[$i]."
      ".$protocol[$i]." ".$user[$i]."\n");
    system("/home/schtorch/diplom/mikehup1 pppconn.sh
      $phone[$i] $protocol[$i] $user[$i] $passwd[$i] &");
    echo "Anwahl\n";
    $a = time()+20;
    $b = time();
    while ($b < $a) {
      $b = time();
    }
    // check if connection exists
    $errstr = exec("ps --no-heading -C capipppd");
    if ($errstr != "") {
      fputs($fp_log, date('Y-m-d H:i:s')." - Verbindung zu
        ".$computer[$i]." besteht\n");
      // download via ftp
      $conn_id = 0;
      $conn_id = ftp_connect("192.168.14.10");
      // login with username and password
      $login_result = ftp_login($conn_id, $user[$i],
        $passwd[$i]);
      // check connection
      if ((!$conn_id) || (!$login_result)) {
        fputs($fp_log, date('Y-m-d H:i:s')." -
          FTP-Verbindung zu ".$computer[$i]." NICHT
          erfolgreich\n");
        break;
      } else {
        fputs($fp_log, date('Y-m-d H:i:s')." -
          FTP-Verbindung zu ".$computer[$i]."
          erfolgreich\n");
      }
      // download the files
      $files = 0;
      $list = ftp_nlist($conn_id, "*.bz2");
      if (isset($list[0])) {
        do {
          $download = ftp_get($conn_id,
            "backup/".$list[$files], $list[$files],
            FTP_BINARY);
          // check download status
          if (!$download) {
            fputs($fp_log, date('Y-m-d H:i:s')." -
              File-Download $list[$files] von
              ".$computer[$i]." NICHT erfolgreich\n");
          } else {
            fputs($fp_log, date('Y-m-d H:i:s')." -
              File-Download $list[$files] von
              ".$computer[$i]." erfolgreich\n");
            ftp_delete($conn_id, $list[$files]);
          }
          $files++;
        }
        while($files < sizeof($list));
      }
      else
      fputs($fp_log, date('Y-m-d H:i:s')." - Keine Dateien
        auf ".$computer[$i]." zum herunterladen\n");
      // close the FTP stream
      ftp_quit($conn_id);
      // close connection
      system("killall -HUP pppd");
      fputs($fp_log, date('Y-m-d H:i:s')." - Verbindung
        beendet\n");
      // unzip data
      shell_exec("cd /home/schtorch/diplom/backup &&
        bunzip2 -k *.bz2");
      fputs($fp_log, date('Y-m-d H:i:s')." - Entpacken
        erfolgreich beendet\n");
      // backup all .bz files
      shell_exec("cd /home/schtorch/diplom/backup && mv
        *.bz2 /home/schtorch/diplom/safe");
      // convert and save data into database
      $cmd = "cd /home/schtorch/diplom/backup && /bin/ls
        *.lst";
      $fp_ls = popen($cmd, "r");
      while ($file_name = trim(fgets($fp_ls, 100))) {
        $erg =
          convertFile("/home/schtorch/diplom/backup/$file_n
          ame");
        fputs($fp_log, date('Y-m-d H:i:s')." -
          convert2mysql.php4: ".$erg);
        unlink("/home/schtorch/diplom/backup/$file_name");
      }
    }
    else
    fputs($fp_log, date('Y-m-d H:i:s')." - Verbindung zu
      ".$computer[$i]." konnte nicht hergestellt werden\n");
    fputs($fp_log, date('Y-m-d H:i:s')." - Ende der
      Skriptabarbeitung für ".$computer[$i]."\n\n");
    echo "Ende $computer[$i]\n";
  }
  fclose($fp_log);
?>



Jens Bierkandt