Subversion Repositories computer_asset_manager_v1

Rev

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