#!/usr/local/bin/perl -w #See https://confluence.slac.stanford.edu/display/IEPM/IEPM+Perl+Coding+Styles #for version of perl to use. # /*---------------------------------------------------------------*/ # /* STANFORD UNIVERSITY NOTICES FOR SLAC SOFTWARE */ # /* ON WHICH COPYRIGHT IS DISCLAIMED */ # /* */ # /* AUTHORSHIP */ # /* This software was created by , Stanford Linear */ # /* Accelerator Center, Stanford University. */ # /* */ # /* ACKNOWLEDGEMENT OF SPONSORSHIP */ # /* This software was produced by the Stanford Linear Accelerator */ # /* Center, Stanford University, under Contract DE-AC03-76SFO0515 */ # /* with the Department of Energy. */ # /* */ # /* GOVERNMENT DISCLAIMER OF LIABILITY */ # /* Neither the United States nor the United States Department of */ # /* Energy, nor any of their employees, makes any warranty, */ # /* express or implied, or assumes any legal liability or */ # /* responsibility for the accuracy, completeness, or usefulness */ # /* of any data, apparatus, product, or process disclosed, or */ # /* represents that its use would not infringe privately owned */ # /* rights. */ # /* */ # /* STANFORD DISCLAIMER OF LIABILITY */ # /* Stanford University makes no representations or warranties, */ # /* express or implied, nor assumes any liability for the use of */ # /* this software. */ # /* */ # /* STANFORD DISCLAIMER OF COPYRIGHT */ # /* Stanford University, owner of the copyright, hereby disclaims */ # /* its copyright and all other rights in this software. Hence, */ # /* anyone may freely use it for any purpose without restriction. */ # /* */ # /* MAINTENANCE OF NOTICES */ # /* In the interest of clarity regarding the origin and status of */ # /* this SLAC software, this and all the preceding Stanford */ # /* University notices are to remain affixed to any copy or */ # /* derivative of this software made or distributed by the */ # /* recipient and are to be affixed to any copy of software made */ # /* or distributed by the recipient that contains a copy or */ # /* derivative of this software. */ # /* */ # /* SLAC Software Notices, Set 4 (OTT.002a, 2004 FEB 03) */ # /*---------------------------------------------------------------*/ # Copyright (c) 2008 # The Board of Trustees of # the Leland Stanford Junior University. All Rights Reserved. ################################################################## # Author: my $authors = "Umar Kalim [kalim at slac.stanford.edu]"; my $releaseDate = "07/07/2009"; my $lastRevised = "07/07/2009"; my $version = "Authors: $authors $releaseDate, version 1.0, Last revised on $lastRevised. "; my $progname = $0 =~ s'^.*/''; ################################################################## # Used to calculate time to execute the script. #my $startTime = time(); #See sub printUsageAndExit() (at bottom) for help use strict; use Sys::Hostname; my $ipaddr = gethostbyname(hostname()); my ($hostname, $aliases, $addrtype, $length, @addrs) = gethostbyaddr($ipaddr, 2); ################################################################## #Used to remove whitespace around a string sub trim { my $string = shift; $string =~ s/^\s+|\s+$//g; return $string; } ###################Main Program#################################### my ($output_file, $input_file, $help, $debug); use Getopt::Long; GetOptions( "o|output=s" => \$output_file, "i|input=s" => \$input_file, "h|help" => \$help, "d|debug" => \$debug, ); if (defined $help) { #Placed here since needs $minimum_rtt to be defined. &printUsageAndExit; } if (defined $debug) { $debug = 1 } else {$debug = 0} unless (defined $output_file) { $output_file = "nodedetails-data.sql" } unless (defined $input_file) { $input_file = "nodedetails.csv" } #############Loading country to region mapping################## open(INFILE, $input_file) or die "Can't open file $input_file: $! \n"; my @lines = ; close INFILE or die "Can't close file $input_file: $!\n"; open(OUTFILE, ">", $output_file) or die "Can't open file >$output_file: $!\n"; my (@tempArray, $line, $lat, $long, $statement, $i); foreach $line (@lines) { chomp $line; ($line, undef) = split(/#/, $line); #Remove comments if (!defined($line) || $line eq "") { next; } @tempArray = split(",", $line); #Get tokens split by tabs $tempArray[7] =~ s/,/ /g; #Remove commas from country ($lat, $long) = split(/ /, $tempArray[9]); for ($i = 1 ; $i < 20 ; $i++) { $tempArray[$i] =~ s/"/ /g; #remove all qoutation marks } if ($debug == 1) { print "\nNodename: " . $tempArray[1]; } $statement = "INSERT INTO `IEPM.NODEDETAILS` VALUES (\"" . $tempArray[1] . "\",\"" #nodename . $tempArray[2] . "\",\"" #ipaddress . $tempArray[3] . "\",\"" #sitename . $tempArray[4] . "\",\"" #nickname . $tempArray[5] . "\",\"" #fullname . $tempArray[6] . "\"," #location . $tempArray[8] . "," #country_ID . $lat . "," #lat . $long . ",\"" #long . $tempArray[10] . "\",\"" #projecttype . $tempArray[11] . "\",\"" #ping server . $tempArray[12] . "\",\"" #traceserver . $tempArray[13] . "\",\"" #dataserver . $tempArray[14] . "\",\"" #url . $tempArray[15] . "\",\"" #gmt . $tempArray[16] . "\",\"" #comments . $tempArray[17] . "\",\"" #app_user . $tempArray[18] . "\",\"" #contacts . $tempArray[19] . "\");\n"; #pingsize print OUTFILE $statement; } close OUTFILE or die "Can't close file $output_file: $!\n"; ################################################################## sub printUsageAndExit() { my $USAGE = "$progname is used to parse nodedetails data and generate an SQL dump file. Usage: $progname [-i nodedetails.csv] [-o nodedetails-data.sql] \n"; print $USAGE; exit; }