Subversion Repositories computer_asset_manager_v1

Rev

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