root / trunk / collect / collect.php @ 29
View | Annotate | Download (30.2 KB)
| 1 | <?php
|
|---|---|
| 2 | # This file is part of MachDB. For license info, read LICENSE
|
| 3 | |
| 4 | include '../include/config.php'; |
| 5 | include '../include/common.php'; |
| 6 | |
| 7 | #print_r($_POST['xml']);
|
| 8 | |
| 9 | $xmlstr = $_POST['xml'];
|
| 10 | $xmlstr = stripslashes($xmlstr); |
| 11 | $xml = new SimpleXMLElement($xmlstr);
|
| 12 | |
| 13 | echo "hostname:", (string) ($xml->hostname), "\n"; |
| 14 | echo "hwid:", (string) ($xml->hwid), "\n"; |
| 15 | |
| 16 | |
| 17 | |
| 18 | #############
|
| 19 | # MAIN
|
| 20 | #############
|
| 21 | |
| 22 | # quick error checks. hwid must exist. also, keep a log of hosts connecting
|
| 23 | if ($xml->hwid) {
|
| 24 | if ($loglevel > 1) { |
| 25 | error_log ("Host " . $xml->hostname . " seen at " . date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)) . "\n", 3, $logfile); |
| 26 | } |
| 27 | } else {
|
| 28 | error_log ("Host " . $xml->hostname ." has no hwid, bailing out!" . date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000)) . "\n", 3, $logfile); |
| 29 | exit;
|
| 30 | } |
| 31 | |
| 32 | $hostname = (string) ($xml->hostname); |
| 33 | $domain = (string) ($xml->domain); |
| 34 | $hwid = (string) ($xml->hwid); |
| 35 | $cpu_count = (int) ($xml->cpu->count); |
| 36 | $swaptotal = (int) ($xml->memory->swaptotal); |
| 37 | $memtotal = (int) ($xml->memory->memtotal); |
| 38 | $os_name = (string) quotemeta($xml->osname); |
| 39 | $os_arch = (string) quotemeta($xml->arch); |
| 40 | $os_basearch = (string) quotemeta($xml->basearch); |
| 41 | $os_kernel = (string) quotemeta($xml->kernel); |
| 42 | $cpu_cache = (int) ($xml->cpu->cache); |
| 43 | $cpu_name = (string) quotemeta($xml->cpu->name); |
| 44 | $cpu_speed = (int) ($xml->cpu->speed); |
| 45 | |
| 46 | # connect to DB
|
| 47 | $db = connect_db($mysqlserver,$mysqluser,$mysqlpassword,$mysqldatabase); |
| 48 | |
| 49 | #print_result(query_db("SELECT host.id,host.hostname,network.macaddr FROM host,network WHERE host.hostname = '$xml->hostname' AND network.macaddr = '$xml->macaddr' AND host.id = network.host_id"));
|
| 50 | |
| 51 | # search for the hardware id to see if it's in the DB
|
| 52 | $host = mysql_fetch_assoc(query_db("SELECT * FROM host WHERE host.hwid = '$hwid'"));
|
| 53 | $os_id = insert_os($os_name,$os_arch,$os_basearch,$os_kernel); |
| 54 | if ($xml->cpu->unified_cache) {
|
| 55 | $cpu_unified_cache = (bool) ($xml->cpu->unified_cache); |
| 56 | } else {
|
| 57 | $cpu_unified_cache = false;
|
| 58 | } |
| 59 | $cpu_id = insert_cpu($cpu_cache,$cpu_name,$cpu_speed,$cpu_unified_cache); |
| 60 | if ($host["hwid"]) { |
| 61 | echo "this host exists with hwid of : $host[hwid]\n"; |
| 62 | $host_id = update_host($hostname,$domain,$hwid,$os_id,$cpu_id,$cpu_count,$swaptotal,$memtotal); |
| 63 | } else {
|
| 64 | echo "this is new hardware\n"; |
| 65 | $host_id = insert_host($hostname,$domain,$hwid,$os_id,$cpu_id,$cpu_count,$swaptotal,$memtotal); |
| 66 | } |
| 67 | if ($xml->nic) { insert_network($host_id,$xml->nic); }
|
| 68 | if ($xml->disk) { machdb_disk($host_id,$xml->disk); }
|
| 69 | if ($xml->filesystem) { machdb_filesystem($host_id,$xml->filesystem); }
|
| 70 | if ($xml->package) { machdb_package($host_id,$xml->package); }
|
| 71 | if ($xml->bios) { machdb_bios($host_id,$xml->bios); }
|
| 72 | if ($xml->system) { machdb_system($host_id,$xml->system); }
|
| 73 | if ($xml->chassis) { machdb_chassis($host_id,$xml->chassis); }
|
| 74 | if ($xml->motherboard) { machdb_mb($host_id,$xml->motherboard); }
|
| 75 | |
| 76 | |
| 77 | ################
|
| 78 | # insert/update filesystem function
|
| 79 | ################
|
| 80 | |
| 81 | function machdb_filesystem($host_id,$filesystem){
|
| 82 | |
| 83 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 84 | #$array = array("device","mountpoint,","size","type");
|
| 85 | foreach($filesystem as $param) { |
| 86 | $device = (string) quotemeta($param->device); |
| 87 | $mountpoint = (string) quotemeta($param->mountpoint); |
| 88 | $size = (int) quotemeta($param->size); |
| 89 | $type = (string) quotemeta($param->type); |
| 90 | |
| 91 | $filesystem_db = mysql_fetch_assoc(query_db("SELECT * FROM filesystem WHERE filesystem.host_id = '$host_id' AND filesystem.device='$device'"));
|
| 92 | $filesystem_xml = array( "device" => (string) $param->device, |
| 93 | "mountpoint" => (string) $param->mountpoint,
|
| 94 | "size" => (string) $param->size,
|
| 95 | "status" => "1", |
| 96 | "type" => (string) $param->type );
|
| 97 | #echo "filesystem_db: "; print_r($filesystem_db); echo "filesystem_xml: "; print_r($filesystem_xml);
|
| 98 | if ($filesystem_db) {
|
| 99 | $updated_filesystem_db = (array_merge($filesystem_db,$filesystem_xml)); |
| 100 | $filesystem_diff = (array_diff_assoc($updated_filesystem_db,$filesystem_db)); |
| 101 | if ($filesystem_diff){
|
| 102 | #echo "$mountpoint diff:\n"; print_r($filesystem_diff);
|
| 103 | echo "$mountpoint has changed, archiving..."; |
| 104 | query_db("INSERT INTO archive_filesystem SELECT * FROM filesystem WHERE id = '$filesystem_db[id]'");
|
| 105 | echo "updating..."; |
| 106 | array_walk($filesystem_diff,'update_filesystem_value',$filesystem_db["id"]); |
| 107 | echo "done.\n"; |
| 108 | } |
| 109 | } else {
|
| 110 | echo "inserting new mountpoint $mountpoint..."; |
| 111 | query_db("INSERT INTO filesystem (`id`,`host_id`,`device`,`mountpoint`,`size`,`type`,`timestamp`) VALUES (NULL,'$host_id','$device','$mountpoint','$size','$type',NULL)");
|
| 112 | echo "done.\n"; |
| 113 | } |
| 114 | } |
| 115 | # process again to disable inactive mounts
|
| 116 | # put all the filesystems from XML in an array
|
| 117 | foreach($filesystem as $param) { |
| 118 | $mountpoint = (string) $param->mountpoint; |
| 119 | $cleanup_xml_filesystem[] = $mountpoint; |
| 120 | } |
| 121 | |
| 122 | # put all the filesystem from db in an array
|
| 123 | $result = query_db("SELECT mountpoint FROM filesystem where host_id = '$host_id' AND status = '1'");
|
| 124 | while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
|
| 125 | foreach ($line as $col_value) { |
| 126 | $cleanup_db_filesystem[] = $col_value; |
| 127 | } |
| 128 | } |
| 129 | if ($cleanup_db_filesystem) {
|
| 130 | # compare the 2 newly created arrays, set status, record history
|
| 131 | #echo "xml:"; print_r($cleanup_xml_filesystem); echo "db:"; print_r($cleanup_db_filesystem);
|
| 132 | $filesystem_diff = (array_diff($cleanup_db_filesystem,$cleanup_xml_filesystem)); |
| 133 | #print_r($filesystem_diff);
|
| 134 | foreach($filesystem_diff as $col_value) { |
| 135 | $mountpoint = $col_value; |
| 136 | echo "$mountpoint no longer used, archiving..."; |
| 137 | query_db("INSERT INTO archive_filesystem SELECT * FROM filesystem WHERE host_id = '$host_id' AND mountpoint = '$mountpoint'");
|
| 138 | echo "setting inactive status..."; |
| 139 | query_db("UPDATE filesystem SET status=0,timestamp=NOW() WHERE host_id = '$host_id' AND mountpoint = '$mountpoint'");
|
| 140 | echo "done\n"; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | } |
| 145 | #############
|
| 146 | |
| 147 | ################
|
| 148 | # insert/update disks
|
| 149 | ################
|
| 150 | |
| 151 | function machdb_disk($host_id,$disk){
|
| 152 | |
| 153 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 154 | # set up an array with all the right info
|
| 155 | #$array = array("device","model,","size","type");
|
| 156 | foreach($disk as $param) { |
| 157 | $device = (string) quotemeta($param->device); |
| 158 | $model = (string) quotemeta($param->model); |
| 159 | $size = (int) ($param->size); |
| 160 | $type = (string) quotemeta($param->type); |
| 161 | # lookup the disk info based on type, model and size
|
| 162 | $disk_db = mysql_fetch_assoc(query_db("SELECT * FROM disk WHERE disk.type = '$type' AND disk.model='$model' AND disk.size = '$size'"));
|
| 163 | if ($disk_db) {
|
| 164 | # if it exists, just set the disk_id so we know what to use
|
| 165 | $disk_id = $disk_db["id"];
|
| 166 | } else {
|
| 167 | # if it doesn't exist, insert a new entry into the disk table and set disk_id
|
| 168 | echo "NEW disk $model, adding...\n"; |
| 169 | query_db("INSERT INTO disk (`id`,`model`,`size`,`type`) VALUES (NULL,'$model','$size','$type')");
|
| 170 | $disk_id = mysql_insert_id(); |
| 171 | echo "done.\n"; |
| 172 | } |
| 173 | |
| 174 | # populate an array with the info from this disk that is in the host_disk map table, if it exists
|
| 175 | $host_disk_db = mysql_fetch_assoc(query_db("SELECT * FROM host_disk WHERE host_id = '$host_id' AND device = '$device'"));
|
| 176 | |
| 177 | if ($host_disk_db) {
|
| 178 | # if it's already in the db, run thru and check for updates and record archive
|
| 179 | $host_disk_xml = array( "device" => (string) $param->device, |
| 180 | "disk_id" => $disk_id,
|
| 181 | "status" => "1", |
| 182 | "host_id" => (string) $host_id);
|
| 183 | $updated_host_disk_db = (array_merge($host_disk_db,$host_disk_xml)); |
| 184 | $host_disk_diff = (array_diff_assoc($updated_host_disk_db,$host_disk_db)); |
| 185 | #echo "$host_disk_db[device] host_disk_db: "; print_r($host_disk_db); echo "host_disk_xml: "; print_r($host_disk_xml); echo "diff:"; print_r($host_disk_diff);
|
| 186 | # if there is a difference, call the update function and record archive
|
| 187 | if ($host_disk_diff){
|
| 188 | #echo $device . "\n"; print_r ($host_disk_diff);
|
| 189 | echo "$device has changed, archiving..."; |
| 190 | query_db("INSERT INTO archive_host_disk SELECT * FROM host_disk WHERE host_disk.id = '$host_disk_db[id]'");
|
| 191 | echo "updating..."; |
| 192 | array_walk($host_disk_diff,'update_host_disk_value',$host_disk_db["id"]); |
| 193 | echo "done\n"; |
| 194 | } |
| 195 | } else {
|
| 196 | # if it's not in the DB, insert it
|
| 197 | echo "NEW $device, adding..."; |
| 198 | query_db("INSERT INTO host_disk (`id`,`host_id`,`disk_id`,`device`,`timestamp`) VALUES (NULL,'$host_id','$disk_id','$device',NULL)");
|
| 199 | echo "done.\n"; |
| 200 | } |
| 201 | |
| 202 | } |
| 203 | |
| 204 | # process again to disable inactive disks
|
| 205 | # put all the disks from XML in an array
|
| 206 | foreach($disk as $param) { |
| 207 | $xml_device = (string) $param->device; |
| 208 | $cleanup_xml_disk[] = $xml_device; |
| 209 | } |
| 210 | |
| 211 | # put all the disk from db in an array
|
| 212 | $result = query_db("SELECT device FROM host_disk where host_id = '$host_id' AND status = '1'");
|
| 213 | while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
|
| 214 | foreach ($line as $col_value) { |
| 215 | $cleanup_db_disk[] = $col_value; |
| 216 | } |
| 217 | } |
| 218 | # compare the 2 newly created arrays, set status, record history
|
| 219 | $disk_diff = (array_diff($cleanup_db_disk,$cleanup_xml_disk)); |
| 220 | #echo "xml:"; print_r($cleanup_xml_disk); echo "db:"; print_r($cleanup_db_disk); echo "diff:"; print_r($disk_diff);
|
| 221 | foreach($disk_diff as $col_value) { |
| 222 | $device = $col_value; |
| 223 | echo "$device no longer used, archiving..."; |
| 224 | query_db("INSERT INTO archive_host_disk SELECT * FROM host_disk WHERE host_id = '$host_id' AND device = '$device'");
|
| 225 | echo "setting inactive status..."; |
| 226 | query_db("UPDATE host_disk SET status=0,timestamp=NOW() WHERE host_id = '$host_id' AND device = '$device'");
|
| 227 | echo "done\n"; |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | #############
|
| 232 | |
| 233 | |
| 234 | ################
|
| 235 | # insert/update network function
|
| 236 | ################
|
| 237 | |
| 238 | function insert_network($host_id,$nic){
|
| 239 | |
| 240 | # shit out if no host_id is found
|
| 241 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 242 | # set up the array for nic and loop thru each interface
|
| 243 | #$array = array("interface","bcast,","macaddr","ipaddr");
|
| 244 | foreach($nic as $param) { |
| 245 | $interface = (string) quotemeta($param->interface);
|
| 246 | $macaddr = (string) quotemeta($param->macaddr); |
| 247 | $netmask = (string) quotemeta($param->netmask); |
| 248 | $broadcast = (string) quotemeta($param->broadcast); |
| 249 | $ipaddr = (string) quotemeta($param->ipaddr); |
| 250 | # get nic info from database, if it exists
|
| 251 | $nic_db = mysql_fetch_assoc(query_db("SELECT * FROM nic WHERE nic.host_id = '$host_id' AND nic.interface='$interface' AND nic.macaddr='$macaddr'"));
|
| 252 | $nic_xml = array( "interface" => (string) $param->interface, |
| 253 | "macaddr" => (string) $param->macaddr,
|
| 254 | "netmask" => (string) $param->netmask,
|
| 255 | "broadcast" => (string) $param->broadcast,
|
| 256 | "host_id" => (string) $host_id,
|
| 257 | "status" => "1", |
| 258 | "ipaddr" => (string) $param->ipaddr );
|
| 259 | # echo "nic_db: "; print_r($nic_db); echo "nic_xml: "; print_r($nic_xml);
|
| 260 | # if it exists, merge and compare the XML and DB arrays
|
| 261 | if ($nic_db) {
|
| 262 | $updated_nic_db = (array_merge($nic_db,$nic_xml)); |
| 263 | $nic_diff = (array_diff_assoc($updated_nic_db,$nic_db)); |
| 264 | if ($nic_diff){
|
| 265 | echo "$interface has changed, archiving..."; |
| 266 | query_db("INSERT INTO archive_nic SELECT * FROM nic WHERE nic.id = '$nic_db[id]'");
|
| 267 | echo "updating..."; |
| 268 | array_walk($nic_diff,'update_nic_value',$nic_db["id"]); |
| 269 | echo "done.\n"; |
| 270 | } |
| 271 | } else {
|
| 272 | # if it doesn't exist, insert new entry in table
|
| 273 | echo "NEW NIC $interface, inserting..."; |
| 274 | query_db("INSERT INTO nic (`id`,`host_id`,`macaddr`,`interface`,`netmask`,`ipaddr`,`broadcast`,`timestamp`) VALUES (NULL,'$host_id','$macaddr','$interface','$netmask','$ipaddr','$broadcast',NULL)");
|
| 275 | echo "done.\n"; |
| 276 | } |
| 277 | } |
| 278 | # process again to disable inactive nics
|
| 279 | # put all the nics from XML in an array
|
| 280 | foreach($nic as $param) { |
| 281 | $interface = (string) $param->interface;
|
| 282 | $cleanup_xml_nic[] = $interface; |
| 283 | } |
| 284 | |
| 285 | # put all the nics from db in an array
|
| 286 | $result = query_db("SELECT interface FROM nic where nic.host_id = '$host_id' AND status = '1'");
|
| 287 | while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
|
| 288 | foreach ($line as $col_value) { |
| 289 | $cleanup_db_nic[] = $col_value; |
| 290 | } |
| 291 | } |
| 292 | # compare the 2 newly created arrays, set status, record history
|
| 293 | #echo "xml:"; print_r($cleanup_xml_nic); echo "db:"; print_r($cleanup_db_nic);
|
| 294 | $nic_diff = (array_diff($cleanup_db_nic,$cleanup_xml_nic)); |
| 295 | foreach($nic_diff as $col_value) { |
| 296 | $interface = $col_value; |
| 297 | echo "$interface no longer used, archiving..."; |
| 298 | query_db("INSERT INTO archive_nic SELECT * FROM nic WHERE host_id = '$host_id' AND interface = '$interface'");
|
| 299 | echo "setting inactive status..."; |
| 300 | query_db("UPDATE nic SET status=0,timestamp=NOW() WHERE host_id = '$host_id' AND interface = '$interface'");
|
| 301 | echo "done\n"; |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | #############
|
| 306 | |
| 307 | ################
|
| 308 | # insert/update bios
|
| 309 | ################
|
| 310 | |
| 311 | function machdb_bios($host_id,$bios){
|
| 312 | echo "BIOS: "; |
| 313 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 314 | # set up an array with all the right info
|
| 315 | $vendor = (string) quotemeta($bios->vendor); |
| 316 | $version = (string) quotemeta($bios->version); |
| 317 | $date = (string) quotemeta($bios->date); |
| 318 | # lookup the package info based on type, model and size
|
| 319 | $bios_db = mysql_fetch_assoc(query_db("SELECT * FROM bios WHERE vendor='$vendor' AND version='$version' AND date='$date'"));
|
| 320 | if ($bios_db) {
|
| 321 | # if it exists, just set the disk_id so we know what to use
|
| 322 | $bios_id = $bios_db["id"];
|
| 323 | } else {
|
| 324 | # if it doesn't exist, insert a new entry into the bios table and set bios_id
|
| 325 | query_db("INSERT INTO bios (`id`,`vendor`,`version`,`date`) VALUES (NULL,'$vendor','$version','$date')");
|
| 326 | echo "NEW : $vendor $version $date ..."; |
| 327 | $bios_id = mysql_insert_id(); |
| 328 | } |
| 329 | |
| 330 | # populate an array with the info from this bios that is in the host_bios map table, if it exists
|
| 331 | $host_bios_db = mysql_fetch_assoc(query_db("SELECT * FROM host_bios WHERE host_id = '$host_id'"));
|
| 332 | if ($host_bios_db) {
|
| 333 | $host_bios_id = $host_bios_db["id"];
|
| 334 | } else {
|
| 335 | # if it's not in the DB, insert it
|
| 336 | query_db("INSERT INTO host_bios (`id`,`host_id`,`bios_id`) VALUES (NULL,'$host_id','$bios_id')");
|
| 337 | $host_bios_id = mysql_insert_id(); |
| 338 | } |
| 339 | |
| 340 | # process again to disable inactive bioss
|
| 341 | # put the bios info from XML in an array
|
| 342 | $xml_host_bios = array ( "host_id" => $host_id, |
| 343 | "bios_id" => $bios_id);
|
| 344 | |
| 345 | # put the bios info from db in an array
|
| 346 | $result = query_db("SELECT host_bios.host_id,host_bios.bios_id FROM host_bios,bios WHERE bios.id = host_bios.bios_id AND host_bios.host_id = '$host_id'");
|
| 347 | $db_host_bios = mysql_fetch_array($result, MYSQL_ASSOC); |
| 348 | |
| 349 | # compare the 2 newly created arrays
|
| 350 | $host_bios_diff = (array_diff($xml_host_bios,$db_host_bios)); |
| 351 | #echo "XML:\n"; print_r($xml_host_bios); echo "DB:\n"; print_r($db_host_bios); echo "diff:\n"; print_r($host_bios_diff);
|
| 352 | if (($host_bios_diff)) {
|
| 353 | # if there is a diff, archive and update
|
| 354 | echo "Chassis has changed, updating...archiving..."; |
| 355 | query_db("INSERT INTO archive_host_bios SELECT * FROM host_bios WHERE id = '$host_bios_id'");
|
| 356 | query_db("UPDATE host_bios SET host_id='$host_id',bios_id='$chassis_id',timestamp=NOW() WHERE host_chassis.id = '$host_bios_id'");
|
| 357 | } |
| 358 | echo "Done\n"; |
| 359 | } |
| 360 | #############
|
| 361 | |
| 362 | ################
|
| 363 | # insert/update chassis
|
| 364 | ################
|
| 365 | |
| 366 | function machdb_chassis($host_id,$chassis){
|
| 367 | echo "Chassis: "; |
| 368 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 369 | # set up an array with all the right info
|
| 370 | $type = (string) quotemeta($chassis->type); |
| 371 | $asset_tag = (string) quotemeta($chassis->asset_tag); |
| 372 | $vendor = (string) quotemeta($chassis->vendor); |
| 373 | $serial = (string) quotemeta($chassis->serial); |
| 374 | # lookup the package info based on type, model and size
|
| 375 | $chassis_db = mysql_fetch_assoc(query_db("SELECT * FROM chassis WHERE type='$type' AND vendor='$vendor'"));
|
| 376 | if ($chassis_db) {
|
| 377 | # if it exists, just set the disk_id so we know what to use
|
| 378 | $chassis_id = $chassis_db["id"];
|
| 379 | } else {
|
| 380 | # if it doesn't exist, insert a new entry into the chassis table and set chassis_id
|
| 381 | query_db("INSERT INTO chassis (`id`,`type`,`vendor`) VALUES (NULL,'$type','$vendor')");
|
| 382 | echo "NEW : $vendor $type ..."; |
| 383 | $chassis_id = mysql_insert_id(); |
| 384 | } |
| 385 | |
| 386 | # populate an array with the info from this chassis that is in the host_chassis map table, if it exists
|
| 387 | $host_chassis_db = mysql_fetch_assoc(query_db("SELECT * FROM host_chassis WHERE host_id = '$host_id'"));
|
| 388 | if ($host_chassis_db) {
|
| 389 | $host_chassis_id = $host_chassis_db["id"];
|
| 390 | } else {
|
| 391 | # if it's not in the DB, insert it
|
| 392 | query_db("INSERT INTO host_chassis (`id`,`host_id`,`chassis_id`,`serial`,`asset_tag`) VALUES (NULL,'$host_id','$chassis_id','$serial','$asset_tag')");
|
| 393 | $host_chassis_id = mysql_insert_id(); |
| 394 | } |
| 395 | |
| 396 | # process again to disable inactive chassiss
|
| 397 | # put the chassis info from XML in an array
|
| 398 | $xml_host_chassis = array ( "host_id" => $host_id, |
| 399 | "chassis_id" => $chassis_id,
|
| 400 | "serial" => $serial,
|
| 401 | "asset_tag" => $asset_tag);
|
| 402 | |
| 403 | # put the chassis info from db in an array
|
| 404 | $result = query_db("SELECT host_chassis.host_id,host_chassis.chassis_id,host_chassis.serial,host_chassis.asset_tag FROM host_chassis,chassis WHERE chassis.id = host_chassis.chassis_id AND host_chassis.host_id = '$host_id'");
|
| 405 | $db_host_chassis = mysql_fetch_array($result, MYSQL_ASSOC); |
| 406 | |
| 407 | # compare the 2 newly created arrays
|
| 408 | $host_chassis_diff = (array_diff($xml_host_chassis,$db_host_chassis)); |
| 409 | #echo "XML:\n"; print_r($xml_host_chassis); echo "DB:\n"; print_r($db_host_chassis); echo "diff:\n"; print_r($host_chassis_diff);
|
| 410 | if (($host_chassis_diff)) {
|
| 411 | # if there is a diff, archive and update
|
| 412 | echo "Chassis has changed, updating...archiving..."; |
| 413 | query_db("INSERT INTO archive_host_chassis SELECT * FROM host_chassis WHERE id = '$host_chassis_id'");
|
| 414 | query_db("UPDATE host_chassis SET host_id='$host_id',chassis_id='$chassis_id',serial='$serial',asset_tag='$asset_tag',timestamp=NOW() WHERE host_chassis.id = '$host_chassis_id'");
|
| 415 | } |
| 416 | echo "Done\n"; |
| 417 | } |
| 418 | |
| 419 | ################
|
| 420 | |
| 421 | ################
|
| 422 | # insert/update motherboard
|
| 423 | ################
|
| 424 | |
| 425 | function machdb_mb($host_id,$mb){
|
| 426 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 427 | echo "Motherboard: "; |
| 428 | # set up an array with all the right info
|
| 429 | #$array = array("device","model,","size","type");
|
| 430 | $name = (string) quotemeta($mb->name); |
| 431 | $version = (string) quotemeta($mb->version); |
| 432 | $vendor = (string) quotemeta($mb->vendor); |
| 433 | $serial = (string) ($mb->serial); |
| 434 | # lookup the package info based on type, model and size
|
| 435 | $mb_db = mysql_fetch_assoc(query_db("SELECT * FROM mb WHERE name = '$name' AND vendor = '$vendor' AND version ='$version'"));
|
| 436 | if ($mb_db) {
|
| 437 | # if it exists, just set the disk_id so we know what to use
|
| 438 | $mb_id = $mb_db["id"];
|
| 439 | } else {
|
| 440 | # if it doesn't exist, insert a new entry into the mb table and set mb_id
|
| 441 | query_db("INSERT INTO mb (`id`,`name`,`vendor`,`version`) VALUES (NULL,'$name','$vendor','$version')");
|
| 442 | echo "NEW: $name"; |
| 443 | $mb_id = mysql_insert_id(); |
| 444 | } |
| 445 | |
| 446 | # populate an array with the info from this mb that is in the host_mb map table, if it exists
|
| 447 | $host_mb_db = mysql_fetch_assoc(query_db("SELECT * FROM host_mb WHERE host_id = '$host_id'"));
|
| 448 | if ($host_mb_db) {
|
| 449 | $host_mb_id = $host_mb_db["id"];
|
| 450 | } else {
|
| 451 | # if it's not in the DB, insert it
|
| 452 | query_db("INSERT INTO host_mb (`id`,`host_id`,`mb_id`,`serial`,`timestamp`) VALUES (NULL,'$host_id','$mb_id','$serial',NULL)");
|
| 453 | $host_mb_id = mysql_insert_id(); |
| 454 | } |
| 455 | |
| 456 | # process again to disable inactive mbs
|
| 457 | # put the mb info from XML in an array
|
| 458 | $xml_host_mb = array ( "mb_id" => $mb_id, |
| 459 | "host_id" => $host_id,
|
| 460 | "serial" => $serial);
|
| 461 | |
| 462 | # put the mb info from db in an array
|
| 463 | $result = query_db("SELECT host_mb.mb_id,host_mb.host_id,host_mb.serial FROM host_mb,mb WHERE mb.id = host_mb.mb_id AND host_mb.host_id = '$host_id'");
|
| 464 | $db_host_mb = mysql_fetch_array($result, MYSQL_ASSOC); |
| 465 | |
| 466 | # compare the 2 newly created arrays
|
| 467 | $host_mb_diff = (array_diff_assoc($db_host_mb,$xml_host_mb)); |
| 468 | #echo "XML:\n"; print_r($xml_host_mb); echo "DB:\n"; print_r($db_host_mb); echo "diff:\n"; print_r($host_mb_diff);
|
| 469 | if (($host_mb_diff)) {
|
| 470 | # if there is a diff, archive and update
|
| 471 | echo "Archiving & Updating..."; |
| 472 | query_db("INSERT INTO archive_host_mb SELECT * FROM host_mb WHERE id = '$host_mb_id'");
|
| 473 | query_db("UPDATE host_mb SET host_id='$host_id',mb_id='$mb_id',serial='$serial',timestamp=NOW() WHERE host_mb.id = '$host_mb_id'");
|
| 474 | } |
| 475 | echo "Done\n"; |
| 476 | |
| 477 | } |
| 478 | ################
|
| 479 | |
| 480 | ################
|
| 481 | # insert/update system
|
| 482 | ################
|
| 483 | |
| 484 | function machdb_system($host_id,$system){
|
| 485 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 486 | echo "System: "; |
| 487 | # set up an array with all the right info
|
| 488 | #$array = array("device","model,","size","type");
|
| 489 | $name = (string) quotemeta($system->name); |
| 490 | $version = (string) quotemeta($system->version); |
| 491 | $vendor = (string) quotemeta($system->vendor); |
| 492 | $serial = (string) quotemeta($system->serial); |
| 493 | $uuid = (string) quotemeta($system->uuid); |
| 494 | # lookup the package info based on type, model and size
|
| 495 | $system_db = mysql_fetch_assoc(query_db("SELECT * FROM system WHERE name = '$name' AND vendor = '$vendor' AND version ='$version'"));
|
| 496 | if ($system_db) {
|
| 497 | # if it exists, just set the disk_id so we know what to use
|
| 498 | $system_id = $system_db["id"];
|
| 499 | } else {
|
| 500 | # if it doesn't exist, insert a new entry into the system table and set system_id
|
| 501 | query_db("INSERT INTO system (`id`,`name`,`vendor`,`version`) VALUES (NULL,'$name','$vendor','$version')");
|
| 502 | echo "NEW: $name"; |
| 503 | $system_id = mysql_insert_id(); |
| 504 | } |
| 505 | |
| 506 | # populate an array with the info from this system that is in the host_system map table, if it exists
|
| 507 | $host_system_db = mysql_fetch_assoc(query_db("SELECT * FROM host_system WHERE host_id = '$host_id'"));
|
| 508 | if ($host_system_db) {
|
| 509 | $host_system_id = $host_system_db["id"];
|
| 510 | } else {
|
| 511 | # if it's not in the DB, insert it
|
| 512 | query_db("INSERT INTO host_system (`id`,`host_id`,`system_id`,`serial`,`uuid`,`timestamp`) VALUES (NULL,'$host_id','$system_id','$serial','$uuid',NULL)");
|
| 513 | $host_system_id = mysql_insert_id(); |
| 514 | } |
| 515 | |
| 516 | # process again to disable inactive systems
|
| 517 | # put the system info from XML in an array
|
| 518 | $xml_host_system = array ( "system_id" => $system_id, |
| 519 | "host_id" => $host_id,
|
| 520 | "serial" => $serial,
|
| 521 | "uuid" => $uuid);
|
| 522 | |
| 523 | # put the system info from db in an array
|
| 524 | $result = query_db("SELECT host_system.system_id,host_system.host_id,host_system.serial,host_system.uuid FROM host_system,system WHERE system.id = host_system.system_id AND host_system.host_id = '$host_id'");
|
| 525 | $db_host_system = mysql_fetch_array($result, MYSQL_ASSOC); |
| 526 | |
| 527 | # compare the 2 newly created arrays
|
| 528 | $host_system_diff = (array_diff($db_host_system,$xml_host_system)); |
| 529 | #echo "XML:\n"; print_r($xml_host_system); echo "DB:\n"; print_r($db_host_system); echo "diff:\n"; print_r($host_system_diff);
|
| 530 | if (($host_system_diff)) {
|
| 531 | # if there is a diff, archive and update
|
| 532 | echo "Archiving & Updating..."; |
| 533 | query_db("INSERT INTO archive_host_system SELECT * FROM host_system WHERE id = '$host_system_id'");
|
| 534 | query_db("UPDATE host_system SET host_id='$host_id',system_id='$system_id',serial='$serial',uuid='$uuid',timestamp=NOW() WHERE host_system.id = '$host_system_id'");
|
| 535 | } |
| 536 | echo "Done\n"; |
| 537 | |
| 538 | } |
| 539 | |
| 540 | ##########################3
|
| 541 | |
| 542 | ################
|
| 543 | # insert/update packages
|
| 544 | ################
|
| 545 | |
| 546 | function machdb_package($host_id,$packages){
|
| 547 | |
| 548 | if (!$host_id){echo "NO HOST ID\n"; die;}; |
| 549 | # set up an array with all the right info
|
| 550 | #$array = array("device","model,","size","type");
|
| 551 | echo "Packages: "; |
| 552 | foreach($packages as $param) { |
| 553 | $name = (string) quotemeta($param->name); |
| 554 | $version = (string) quotemeta($param->version); |
| 555 | $release = (string) quotemeta($param->release); |
| 556 | if (!$release) { $release = "0"; }; |
| 557 | $group = (string) quotemeta($param->group); |
| 558 | $arch = (string) quotemeta($param->arch); |
| 559 | # lookup the package info based on type, model and size
|
| 560 | $pkg_db = mysql_fetch_assoc(query_db("SELECT * FROM `pkg` WHERE pkg.name = '$name' AND `arch` = '$arch' AND `version` ='$version' AND `release` ='$release'"));
|
| 561 | if ($pkg_db) {
|
| 562 | # if it exists, just set the disk_id so we know what to use
|
| 563 | $pkg_id = $pkg_db["id"];
|
| 564 | } else {
|
| 565 | # if it doesn't exist, insert a new entry into the pkg table and set pkg_id
|
| 566 | $db_string="INSERT INTO pkg (`id`,`name`,`arch`,`version`,`release`) VALUES (NULL,'$name','$arch','$version','$release')";
|
| 567 | #echo "Running $db_string \n";
|
| 568 | query_db($db_string); |
| 569 | |
| 570 | echo "N"; |
| 571 | $pkg_id = mysql_insert_id(); |
| 572 | } |
| 573 | |
| 574 | # populate an array with the info from this pkg that is in the host_pkg map table, if it exists
|
| 575 | $host_pkg_db = mysql_fetch_assoc(query_db("SELECT * FROM host_pkg WHERE host_id = '$host_id' AND pkg_id = '$pkg_id'"));
|
| 576 | |
| 577 | if ($host_pkg_db) {
|
| 578 | #echo ".";
|
| 579 | } else {
|
| 580 | # if it's not in the DB, insert it
|
| 581 | query_db("INSERT INTO host_pkg (`id`,`host_id`,`pkg_id`,`timestamp`) VALUES (NULL,'$host_id','$pkg_id',NULL)");
|
| 582 | echo "+"; |
| 583 | } |
| 584 | |
| 585 | } |
| 586 | |
| 587 | |
| 588 | # process again to disable inactive pkgs
|
| 589 | # put all the pkgs from XML in an array
|
| 590 | foreach($packages as $param) { |
| 591 | $xml_name = (string) $param->name; |
| 592 | $xml_version = (string) $param->version; |
| 593 | $xml_release = (string) $param->release; |
| 594 | if (!$xml_release) { $release = "0"; }; |
| 595 | $group = (string) quotemeta($param->group); |
| 596 | $xml_arch = (string) $param->arch; |
| 597 | $cleanup_xml_pkg[] = "$xml_name\t$xml_version\t$xml_release\t$xml_arch";
|
| 598 | } |
| 599 | |
| 600 | # put all the pkg from db in an array
|
| 601 | $result = query_db("SELECT pkg.name,pkg.version,pkg.release,pkg.arch FROM pkg,host_pkg WHERE pkg.id = host_pkg.pkg_id AND host_pkg.host_id = '$host_id'");
|
| 602 | while ($db_pkg = mysql_fetch_array($result, MYSQL_ASSOC)) {
|
| 603 | $db_name = $db_pkg["name"];
|
| 604 | $db_version = $db_pkg["version"];
|
| 605 | $db_release = $db_pkg["release"];
|
| 606 | $db_arch = $db_pkg["arch"];
|
| 607 | $cleanup_db_pkg[] = "$db_name\t$db_version\t$db_release\t$db_arch";
|
| 608 | } |
| 609 | # compare the 2 newly created arrays, delete old, record history
|
| 610 | #echo "XML:\n"; print_r($cleanup_xml_pkg); echo "DB:\n"; print_r($cleanup_db_pkg); print_r($pkg_diff);
|
| 611 | $pkg_diff = (array_diff($cleanup_db_pkg,$cleanup_xml_pkg)); |
| 612 | foreach($pkg_diff as $string) { |
| 613 | list($name,$version,$release,$arch) = split("\t", $string); |
| 614 | $host_pkg = mysql_fetch_assoc(query_db("SELECT id FROM pkg WHERE `name` = '$name' AND `version` = '$version' AND `arch` = '$arch' AND `release` = '$release'"));
|
| 615 | $pkg_id = $host_pkg["id"];
|
| 616 | # echo "$name: $version\n";
|
| 617 | query_db("INSERT INTO archive_host_pkg SELECT * FROM host_pkg WHERE host_id = '$host_id' AND pkg_id = '$pkg_id'");
|
| 618 | query_db("DELETE FROM host_pkg WHERE host_id = '$host_id' AND pkg_id = '$pkg_id'");
|
| 619 | query_db("UPDATE archive_host_pkg SET timestamp=NOW() WHERE host_id = '$host_id' AND pkg_id = '$pkg_id'");
|
| 620 | echo "-"; |
| 621 | } |
| 622 | |
| 623 | echo " Done\n"; |
| 624 | |
| 625 | } |
| 626 | |
| 627 | ##########################3
|
| 628 | |
| 629 | |
| 630 | ################
|
| 631 | # update host function
|
| 632 | ################
|
| 633 | |
| 634 | function update_host($hostname,$domain,$hwid,$os_id,$cpu_id,$cpu_count,$swaptotal,$memtotal){
|
| 635 | |
| 636 | $host_db = mysql_fetch_assoc(query_db("SELECT * FROM host WHERE host.hwid='$hwid'"));
|
| 637 | $host_xml = array( "hostname" => $hostname, |
| 638 | "domain" => $domain,
|
| 639 | "hwid" => $hwid,
|
| 640 | "os_id" => $os_id,
|
| 641 | "cpu_id" => $cpu_id,
|
| 642 | "cpu_count" => $cpu_count,
|
| 643 | "swaptotal" => $swaptotal,
|
| 644 | "memtotal" => $memtotal,
|
| 645 | ); |
| 646 | $updated_host_db = (array_merge($host_db,$host_xml)); |
| 647 | $host_diff = (array_diff_assoc($updated_host_db,$host_db)); |
| 648 | if ($host_diff){
|
| 649 | echo "host id " . $host_db["id"] . " has changed, archiving..."; |
| 650 | query_db("INSERT INTO archive_host SELECT * FROM host WHERE host.id = '$host_db[id]'");
|
| 651 | echo "updating..."; |
| 652 | array_walk($host_diff,'update_host_value',$host_db["id"]); |
| 653 | echo "done.\n"; |
| 654 | |
| 655 | } |
| 656 | return $host_db["id"]; |
| 657 | } |
| 658 | ################
|
| 659 | |
| 660 | ################
|
| 661 | # insert host function
|
| 662 | ################
|
| 663 | |
| 664 | function insert_host($hostname,$domain,$hwid,$os_id,$cpu_id,$cpu_count,$swaptotal,$memtotal){
|
| 665 | |
| 666 | $query = "INSERT INTO `host` (`id`,`hostname`,`domain`,`hwid`,`os_id`,`cpu_id`,`cpu_count`,`swaptotal`,`memtotal`,`location_id`,`status`,`timestamp`) VALUES (NULL,'$hostname','$domain','$hwid','$os_id','$cpu_id','$cpu_count','$swaptotal','$memtotal','1',NULL,CURRENT_TIMESTAMP)";
|
| 667 | |
| 668 | #echo $query,"\n";
|
| 669 | $insert_query = query_db($query); |
| 670 | $host_id = mysql_insert_id(); |
| 671 | echo "NEW host inserted as host_id:" . $host_id . "\n"; |
| 672 | return $host_id;
|
| 673 | } |
| 674 | ################
|
| 675 | |
| 676 | |
| 677 | ################
|
| 678 | # cpu function
|
| 679 | ################
|
| 680 | function insert_cpu($cpu_cache,$cpu_name,$cpu_speed){
|
| 681 | if (func_num_args() == 4) { |
| 682 | $unified_cache = func_get_arg(3);
|
| 683 | } |
| 684 | $cpu = mysql_fetch_assoc(query_db("SELECT * FROM cpu WHERE cpu.cache = '$cpu_cache' AND cpu.name = '$cpu_name' AND cpu.speed = '$cpu_speed' AND cpu.unified_cache IS " . ($unified_cache ? "NOT" : "") . " NULL")); |
| 685 | $cpu_id = $cpu["id"];
|
| 686 | if ($cpu["id"]) { |
| 687 | echo "$cpu_name @ $cpu_speed Mhz id: $cpu[id]\n"; |
| 688 | } else {
|
| 689 | echo "NEW CPU $cpu_name @ $cpu_speed Mhz, inserting into DB..."; |
| 690 | query_db("INSERT INTO `cpu` (`id`,`name`,`speed`,`cache`, `unified_cache`) VALUES (NULL,'$cpu_name','$cpu_speed','$cpu_cache'," . ($unified_cache ? 1 : "NULL") . ")"); |
| 691 | echo "done.\n"; |
| 692 | # get the cpu_id newly created
|
| 693 | $cpu_id = mysql_insert_id(); |
| 694 | } |
| 695 | return $cpu_id;
|
| 696 | } |
| 697 | ################
|
| 698 | |
| 699 | |
| 700 | |
| 701 | ################
|
| 702 | # os function
|
| 703 | ################
|
| 704 | |
| 705 | function insert_os($os_name,$os_arch,$os_basearch,$os_kernel) {
|
| 706 | $os = mysql_fetch_assoc(query_db("SELECT * FROM os WHERE os.name = '$os_name' AND os.arch = '$os_arch' AND os.basearch = '$os_basearch' AND os.kernel = '$os_kernel'"));
|
| 707 | $os_id = $os["id"];
|
| 708 | if ($os["id"]) { |
| 709 | echo "$os_name id: $os[id]\n"; |
| 710 | } else {
|
| 711 | echo "NEW OS $os_name, inserting..."; |
| 712 | query_db("INSERT INTO `os` (`id`,`name`,`arch`,`basearch`,`kernel`) VALUES (NULL,'$os_name','$os_arch','$os_basearch','$os_kernel')\n");
|
| 713 | echo "done.\n"; |
| 714 | # get the os_id
|
| 715 | $os_id = mysql_insert_id(); |
| 716 | } |
| 717 | return $os_id;
|
| 718 | } |
| 719 | ################
|
| 720 | |
| 721 | |
| 722 | ################
|
| 723 | # function for use with the update_host array walk
|
| 724 | ################
|
| 725 | |
| 726 | function update_host_value($value, $key,$host_id) {
|
| 727 | query_db("UPDATE host SET $key = '$value',timestamp = NOW() WHERE host.id = '$host_id'");
|
| 728 | #echo "updated $key : $value \n";
|
| 729 | } |
| 730 | |
| 731 | ################
|
| 732 | |
| 733 | |
| 734 | ################
|
| 735 | # function for use with the insert_nic array walk
|
| 736 | ################
|
| 737 | function update_nic_value($value, $key,$nic_id) {
|
| 738 | query_db("UPDATE nic SET $key = '$value',timestamp = NOW(), status = '1' WHERE nic.id = '$nic_id'");
|
| 739 | #echo "updated $key : $value \n";
|
| 740 | } |
| 741 | ################
|
| 742 | |
| 743 | ################
|
| 744 | # function for use with the machdb_packages array walk
|
| 745 | ################
|
| 746 | function update_host_pkg_value($value, $key,$host_pkg_id) {
|
| 747 | query_db("UPDATE host_pkg SET $key = '$value',timestamp = NOW() WHERE id = '$host_pkg_id'");
|
| 748 | #echo "updated $key : $value \n";
|
| 749 | } |
| 750 | ################
|
| 751 | |
| 752 | ################
|
| 753 | # function for use with the machdb_disk array walk
|
| 754 | ################
|
| 755 | function update_host_disk_value($value, $key,$host_disk_id) {
|
| 756 | query_db("UPDATE host_disk SET $key = '$value',timestamp = NOW() WHERE id = '$host_disk_id'");
|
| 757 | #echo "updated $key : $value \n";
|
| 758 | } |
| 759 | ################
|
| 760 | |
| 761 | ################
|
| 762 | # function for use with the machdb_filesystem array walk
|
| 763 | ################
|
| 764 | function update_filesystem_value($value, $key,$filesystem_id) {
|
| 765 | query_db("UPDATE filesystem SET $key = '$value',timestamp = NOW() WHERE id = '$filesystem_id'");
|
| 766 | #echo "updated $key : $value \n";
|
| 767 | } |
| 768 | ################
|
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | close_db($db); |
| 776 | |
| 777 | echo "\n\nFinished\n"; |
| 778 | |
| 779 | |
| 780 | ?> |
| 781 |