Subversion Repositories computer_asset_manager_v1

Rev

Rev 24 | Rev 28 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
23 rodolico 1
<?php
2
 
3
/*
4
 * Requires php-pear and libyaml under Debian Wheezy
5
 *
6
 * apt-get install php-pear php5-dev libyaml-dev libyaml-0-2 libyaml-0-2-dbg
7
 * pecl install yaml
8
 * echo 'extension=yaml.so' >> /etc/php5/cli/php.ini
9
 * echo 'extension=yaml.so' >> /etc/apache2/cli/php.ini
10
 * 
25 rodolico 11
 * truncate table backups_run
24 rodolico 12
 * alter table backups_run add skipped bigint(20),add file_name varchar(60),add notes text;
13
 * alter table backups_run modify end_time  datetime null, modify start_time datetime null;
25 rodolico 14
 * alter table backups_run add added_date datetime not null;
23 rodolico 15
 * 
16
*/
17
 
25 rodolico 18
   /*
19
    * This will return error codes
20
    * 1 - Invalid Data File Name
21
    * 2 - Could not open the file
22
    * 3 - Duplicate Report
23
    * 4 - Server Not Found
24
    */
25
 
26
 
23 rodolico 27
require 'library.php';
24 rodolico 28
$VERSION='0.1.0';
23 rodolico 29
 
24 rodolico 30
$results = array();
23 rodolico 31
 
24 rodolico 32
function parseFile( $filename, $path, $maxLineLength = 4096 ) {
33
   $data = array ( 
34
                     'server_name' => '',
35
                     'report_date' => '',
36
                     'start_time' => '',
37
                     'version' => '',
38
                     'end_time' => '',
39
                     'files_count' => '',
40
                     'files_size' => '',
41
                     'transferred_count' => 0,
42
                     'transferred_size' => '',
43
                     'files_deleted' => 0,
44
                     'skipped' => 0,
45
                     'data_sent' => '',
46
                     'data_received' => '',
47
                     'disk_used' => '',
48
                     'notes' => '',
49
   );
23 rodolico 50
 
51
 
24 rodolico 52
   $matches = array();
23 rodolico 53
 
24 rodolico 54
   $line;
23 rodolico 55
 
24 rodolico 56
   $data['file_name'] = $filename;
57
   # get server name from the file name, which should be
58
   # datetime_servername.backup.log
25 rodolico 59
   if ( preg_match( '/(\d+)_([a-z0-9_.-]+)\.backup\.log/', $filename, $matches ) ) {
24 rodolico 60
      $data['report_date'] = $matches[1];
61
      $data['server_name'] = $matches[2];
62
   } else {
63
      # ensure only three parts returned
64
      $data['error'] = "1\tInvalid Data File Name";
65
      return $data;
23 rodolico 66
   }
67
 
24 rodolico 68
   $log = fopen ( $path . '/' . $filename , 'rb' );
69
   if ( $log ) {
70
      // get through the header lines
71
      while (( $line = fgets( $log, $maxLineLength )) !== false ) {
72
         if ( preg_match( '/^backup\s+v?([\d.]+)$/', $line, $matches ) ) {
73
            $data['version'] = $matches[1];
74
         }
75
         // this line is the end of the header lines
76
         if ( preg_match( '/sending incremental file list/', $line, $matches ) ) {
77
            break;
78
         }
23 rodolico 79
      }
80
 
24 rodolico 81
      // count line by line processing information. These are the actual lines
82
      // indicating files checked and how they were processed
83
      while (( $line = fgets( $log, $maxLineLength )) !== false ) {
84
         $line = rtrim($line);
85
         if ( $line ) {
86
            if ( ! preg_match( '~/$~', $line ) ) // count lines not ending in /, ie not directories
87
               $data['transferred_count']++;
88
            if ( preg_match( '/^deleting /', $line ) ) // count number deleted
89
               $data['files_deleted']++;
90
            if ( preg_match( '/^skipping /', $line ) )  // count number skipped
91
               $data['skipped']++ ;
92
         } else {
93
            break;
94
         }
23 rodolico 95
      }
24 rodolico 96
      $data['transferred_count'] -= $data['files_deleted'] + $data['skipped'];
23 rodolico 97
 
24 rodolico 98
      // we should have a stats summary at the end
99
      while (( $line = fgets( $log, $maxLineLength )) !== false ) {
100
         $data['notes'] .= $line; // everything here goes into notes
101
         if ( preg_match( '/Number of files:\s+(\d+)/', $line, $matches ) ) {
102
            $data['files_count'] = $matches[1];
103
         } elseif ( preg_match( '/Number of files transferred:\s+(\d+)/', $line, $matches ) ) {
104
            $data['transferred_count'] = $matches[1];
105
         } elseif ( preg_match( '/Total file size:\s+(\d+) bytes/', $line, $matches )  ) {
106
            $data['files_size'] = $matches[1];
107
         } elseif ( preg_match( '/Total transferred file size:\s+(\d+) bytes/', $line, $matches )  ) {
108
            $data['transferred_size'] = $matches[1];
109
         } elseif ( preg_match( '/Total bytes sent:\s+(\d+)/', $line, $matches )  ) {
110
            $data['data_sent'] = $matches[1];
111
         } elseif ( preg_match( '/Total bytes received:\s+(\d+)/', $line, $matches )  ) {
112
            $data['data_received'] = $matches[1];
113
         } elseif ( preg_match( '/Backup Version\s+v?([\d.]+)/', $line, $matches )  ) {
114
            // this is the end of the rsync stats summary, and the
115
            // beginning of the rsbackup summary
116
            $data['version'] = $matches[1];
117
            break;
118
         }
23 rodolico 119
      }
120
 
24 rodolico 121
      // process rsbackup summary
122
      while (( $line = fgets( $log, $maxLineLength )) !== false ) {
123
         $data['notes'] .= $line;
124
         if ( preg_match( '/^Begin\s+(\d+)/', $line, $matches )   ) {
125
            $data['start_time'] = $matches[1];
126
         } elseif ( preg_match( '/Complete\s+(\d+)/', $line, $matches )   ) {
127
            $data['end_time'] = $matches[1];
128
         } elseif ( preg_match( '/^Status:/', $line, $matches )   ) {
129
            break;
23 rodolico 130
         }
131
      }
24 rodolico 132
      // the rest of it just goes into notes
133
      while (( $line = fgets( $log, $maxLineLength )) !== false ) {
134
         $data['notes'] .= $line;
23 rodolico 135
      }
24 rodolico 136
      fclose ( $log );
137
   } else {
138
      $data['error'] = "2\tCould not open file name $filename for read";
23 rodolico 139
   }
24 rodolico 140
   return $data;
23 rodolico 141
 
24 rodolico 142
}
23 rodolico 143
 
144
 
24 rodolico 145
function recordMessage( $message ) {
146
   global $results;
147
   $results[] = $message;
148
}
23 rodolico 149
 
150
 
24 rodolico 151
/* 
152
 * checks for a duplicate report, ie one that has already been run.
153
 * if this computer has a report already for this date/time
154
 */ 
155
function checkDuplicate( $backups_id, $report_date) {
156
   $sql = "select count(*) from backups_run where backups_id = $backups_id and report_date = $report_date";
157
   $count = getOneDBValue( $sql );
158
   return $count ? "3\tDuplicate Report for $backups_id on $report_date" : null;
159
} // recordReport
23 rodolico 160
 
161
 
24 rodolico 162
function findServer( $name ) {
163
   $sql = "select distinct( device_id ) from (select device_id from device where name = '$name' union select device_id from device_alias where alias = '$name') a";
164
   $device_id = getOneDBValue( $sql );
165
   if ( ! isset( $device_id ) ) {
166
      recordMessage( "Could not locate device $name, not processing record" );
167
      return null;
23 rodolico 168
   }
24 rodolico 169
   $sql = "select backups_id from backups where device_id = $device_id";
170
   $backups_id = getOneDBValue( $sql );
171
   if ( isset( $backups_id ) ) {
172
      return $backups_id;
173
   } else {
174
      recordMessage( "Adding server $name to backups" );
175
      $sql = "insert into backups ( device_id,backups_server_id,notes ) values ( $device_id, 1, 'Automatically Added' )";
176
      $results = queryDatabaseExtended( $sql );
177
      return $results['insert_id'];
178
   }
179
}
23 rodolico 180
 
181
 
24 rodolico 182
/* 
183
 * Creates an entry in the backups_run table
184
 */ 
185
function recordReport( $report ) {
186
 
187
   $keys = array();
188
   $values = array();
189
 
190
   foreach ( $report as $keyfield => $value ) {
191
      if ( $keyfield == 'server_name' ) {
192
         $keys[] = 'backups_id';
193
         $device_id = findServer( $value );
194
         if ( isset( $device_id ) ) {
195
            $values[] = $device_id;
196
            $duplicate = checkDuplicate( $device_id, $report['report_date'] );
197
            if ( $duplicate )
198
               return $report['file_name'] . "\t$duplicate";
199
         } else {
200
            return $report['file_name'] . "\t4\tServer not found";
23 rodolico 201
         }
24 rodolico 202
      } else {
203
         $keys[] = $keyfield;
204
         $values[] = makeSafeSQLValue( $value );
23 rodolico 205
      }
24 rodolico 206
   }
25 rodolico 207
   $keys[] = 'added_date'; $values[] = 'now()';
24 rodolico 208
   $sql = 'insert into backups_run (' . implode( ',', $keys ) . ') values (' . implode( ',', $values ) . ')';
209
   // if we made it this far, we are ok, so just add the report id
210
   $result = queryDatabaseExtended( $sql );
211
   return $report['file_name'] . "\t0\tok";
212
} // recordReport
23 rodolico 213
 
214
 
215
/*
216
 * we don't know where the configuration file will be, so we have a list
217
 * below (searchPaths) to look for it. It will load the first one it 
218
 * finds, then exit. THUS, you could have multiple configuration files
219
 * but only the first one in the list will be used
220
 */
221
 
24 rodolico 222
$confFileName = "rsbackupRead.conf.yaml";
223
$searchPaths = array( '/etc/camp', '/opt/camp', '/opt/camp/rsbackup', $_SERVER['SCRIPT_FILENAME'], getcwd() );
23 rodolico 224
$configuration = array();
225
foreach ( $searchPaths as $path ) {
226
   if ( file_exists( "$path/$confFileName" ) ) {
227
      $configuration = yaml_parse_file( "$path/$confFileName" );
228
      break; // exit out of the loop; we don't try to load it more than once
229
   } // if
230
} // foreach
231
 
24 rodolico 232
 
23 rodolico 233
mysql_connect( $configuration['database']['databaseServer'], $configuration['database']['databaseUsername'], $configuration['database']['databasePassword'] ) or die(mysql_error());
234
mysql_select_db( $configuration['database']['database'] ) or die(mysql_error());
235
 
236
 
237
 
24 rodolico 238
for ( $i = 1 ; $i < count( $argv ); $i++ ) {
239
   $filename = $argv[$i];
240
   $report = parseFile( $filename, $configuration['datapath'] . '/' . $configuration['unprocessed_path'] );
241
   if ( isset( $report['error'] ) ) { // we had an error processing the report
25 rodolico 242
      $result[] = "$filename\t" . $report['error']; 
24 rodolico 243
   } else {
244
      $result[] = recordReport( $report );
245
   }
23 rodolico 246
}
247
 
24 rodolico 248
print implode( "\n", $result );
249
 
23 rodolico 250
?>