#!/usr/bin/env php 0; } ## # geocodeAddress(): Use Google Geocode API to turn addresses into GPS coordinates # Requires an API key, which must be provided with the --geocode-api-key parameter ## function geocodeAddress($siteAddr1, $siteAddr2, $siteCity, $siteZip, $siteCountry) { global $verbose, $debug, $quiet, $geocodeApiKey, $httpsProxy; $latGeocode = ''; $lngGeocode = ''; $googleApiUrl = 'https://maps.googleapis.com/maps/api/geocode/json'; if (!$geocodeApiKey) { # Dont even try without the key displayHelp('Error: --geocode-api-key must be given with --geocode-address'); } $requestUrl = sprintf('%s?address=%s,%s,%s,%s&key=%s', $googleApiUrl, urlencode($siteAddr1), urlencode($siteAddr2), urlencode($siteCity), urlencode($siteCountry), $geocodeApiKey); if ($verbose || $debug) { echoQuiet("Geocode URL: $requestUrl\n"); } $result = fetchCurl($requestUrl); if ($result) { $jsonResult = json_decode($result); if ($debug) { echoQuiet('Result was: ' . print_r($jsonResult, 1)); } if ($jsonResult && isset($jsonResult->results[0])) { $latGeocode = $jsonResult->results[0]->geometry->location->lat; $lngGeocode = $jsonResult->results[0]->geometry->location->lng; if (!$quiet) { echoQuiet("Geocoded Coordinates: $latGeocode,$lngGeocode\n"); } } else { if (!$quiet) { echoQuiet("Error: Query to URL: $requestUrl failed.\n"); } } } return (array($latGeocode, $lngGeocode)); } function fixCoordinates($lat, $lng) { $utfCoord = utf8_decode("$lat $lng"); # Normalise the characters to put them through a regex if (preg_match('/(\d+)\xB0(\d+)\'((?:[.]\d+|\d+(?:[.]\d*)?))"?([NS]) +(\d+)\xB0(\d+)\'((?:[.]\d+|\d+(?:[.]\d*)?))"?([EW])/', $utfCoord, $matches)) { array_shift($matches); # Get rid of $matches[0] list($degN, $minN, $secN, $NS, $degE, $minE, $secE, $EW) = $matches; # Get the matches from the regex $lat = sprintf('%0.6f', ($NS == 'S' ? -1 : 1) * ($degN + ($minN / 60) + ($secN / 3600))); $lng = sprintf('%0.6f', ($EW == 'W' ? -1 : 1) * ($degE + ($minE / 60) + ($secE / 3600))); } return (array($lat, $lng)); } /* displayVersion - displays version information */ function displayVersion() { $version = get_cacti_cli_version(); echoQuiet("Cacti Add Site Utility, Version $version, " . COPYRIGHT_YEARS . "\n"); } function displayHelp($errorMessage = null) { global $log; $log = false; displayVersion(); if ($errorMessage) { echoQuiet("$errorMessage\n\n"); } echoQuiet("\nUsage: add_site.php [site-options] [--quiet]\n\n"); echoQuiet("Site options:\n"); echoQuiet(" --name=[Site Name] e.g. 'Telehouse East'\n"); echoQuiet(" --addr1=[Address Line 1] e.g. 'Coriander Road'\n"); echoQuiet(" --addr2=[Address Line 2] e.g. 'Poplar'\n"); echoQuiet(" --city=[City] e.g. 'London''\n"); echoQuiet(" --state=[State] e.g. 'London'\n"); echoQuiet(" --postcode=[Zip or Postcode] e.g. 'E14 2AA'\n"); echoQuiet(" --country=[Country] e.g. 'United Kingdom'\n"); echoQuiet(" --timezone=[Timezone] e.g. 'Europe/London'\n"); echoQuiet(" --latitude=[Latitutude] e.g. '51.5115172'\n"); echoQuiet(" --longitude=[Longitude] e.g. '-0.0017868'\n"); echoQuiet(" --alt-name=[Alt. Name] e.g. 'LINX Telehouse'\n"); echoQuiet(" --notes=[Site Notes] e.g. 'Email: support@telehouse.net'\n\n"); echoQuiet("Geocoding Options:\n"); echoQuiet(" --geocode Try to turn addresses into GPS coordinates\n"); echoQuiet(" --geocode-api-key Your Google API key - https://developers.google.com/maps/documentation/geocoding/get-api-key\n"); echoQuiet(" --proxy Proxy server to use in http://proxy.server:port format\n\n"); echoQuiet("Device Map Options:\n"); echoQuiet(" --device-map-regex=[regular expression] e.g.'rtr-th[e|w]-pe\d'\n"); echoQuiet(" --device-map-wildcard=[mysql like] e.g.'rtr-%the%-pe%'\n\n"); echoQuiet(" --ip-map-regex=[regular expression] e.g. '172.31.224.[1-8]'\n"); echoQuiet(" --ip-map-wildcard=[mysql like] e.g.'172.31.224.%'\n"); echoQuiet(" --do-map Do the mapping.\n\n"); echoQuiet("General Options:\n"); echoQuiet(" --quiet Keep it quiet\n"); echoQuiet(" --no-replace Allow duplicate site names to be created\n\n"); echoQuiet("Notes:\n"); echoQuiet(" By default, sites with the same name will be updated rather than added.\n"); echoQuiet(" This can be disabled with --no-replace\n\n"); echoQuiet(" GPS coordinates should preferably be in dotted decimal format,\n"); echoQuiet(" if supplied in DMS format, a conversion will be attempted, but\n"); echoQuiet(" your mileage may vary.\n\n"); echoQuiet(" Devices can be mapped to the site by providing either regular expression\n"); echoQuiet(" or MySQL wildcard against the host description or IP address.\n\n"); echoQuiet(" By default, only matching devices will be shown, to actually make\n"); echoQuiet(" the changes, use the --do-map option. This is to mistaken updates,\n"); echoQuiet(" please check your filters work first!\n\n"); echoQuiet(" There are some macros which will be expanded in the --notes field:\n\n"); echoQuiet(" %DATE% - The current date in mysql format\n"); echoQuiet(" %TIME% - The current time in mysql format\n"); echoQuiet(" %GOOGLE_MAPS_URL% - The link to Google Maps for this sites GPS coordinates\n\n"); exit; } function echoQuiet($str, $level = 0) { global $quiet, $log; if (!$quiet) { print("$str"); } if ($log) { $str = preg_replace('/^[\n| ]?+/', '', $str); cacti_log($str, false, 'ADD_SITE:', $level); } } function fetchCurl($url) { global $verbose, $debug, $httpsProxy; if (!function_exists('curl_init')) { displayHelp("Error: cURL must be enabled in PHP if --geocode is specified.\nSee http://php.net/manual/en/curl.setup.php for help.\n"); } $curl = curl_init(); $header[0] = 'Accept: text/xml,application/xml,application/json,application/xhtml+xml,'; $header[0] .= 'text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; $header[] = 'Cache-Control: max-age=0'; $header[] = 'Connection: keep-alive'; $header[] = 'Keep-Alive: 300'; $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'; $header[] = 'Accept-Language: en-us,en;q=0.5'; curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_TIMEOUT, 10); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, 0); if ($httpsProxy) { if ($verbose || $debug) { echoQuiet("Using HTTPS proxy: $httpsProxy\n"); } curl_setopt($curl, CURLOPT_PROXY, $httpsProxy); } $buffer = curl_exec($curl); curl_close($curl); return $buffer; }