|
| 1 | +#!/usr/bin/php |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * This script checks if the file shares at Ringsted are available, and makes |
| 5 | + * sure they are mounted. On top of this, the script also checks what the last |
| 6 | + * file is. Upon errors, it will send them by mail, and later by POST when our |
| 7 | + * event console is up and running. |
| 8 | + * |
| 9 | + * Maintained and written by Bellcom's Operations |
| 10 | + * |
| 11 | + * Last revision: |
| 12 | + * Jan 26 2021 |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +if (PHP_SAPI !== 'cli') { |
| 17 | + return; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Configuration |
| 22 | + */ |
| 23 | + |
| 24 | +/** Configuration */ |
| 25 | +$shortopts = "h::s::p::c::m::l::d::"; |
| 26 | +$longopts = ["host", "sharedArray", "port", "cred", "mountPath", "log", "debug"]; |
| 27 | +$opts = getopt($shortopts, $longopts); |
| 28 | +$port = 445; |
| 29 | +$debug = FALSE; |
| 30 | +foreach ($opts as $key => $value) { |
| 31 | + switch ($key) { |
| 32 | + case 'h': |
| 33 | + case 'host': |
| 34 | + $host = $value; |
| 35 | + break; |
| 36 | + |
| 37 | + case 's': |
| 38 | + case 'sharedArray': |
| 39 | + $sharesArray = explode(',', $value); |
| 40 | + break; |
| 41 | + |
| 42 | + case 'p': |
| 43 | + case 'port': |
| 44 | + $port = $value; |
| 45 | + break; |
| 46 | + |
| 47 | + case 'c': |
| 48 | + case 'cred': |
| 49 | + $credentials = $value; |
| 50 | + break; |
| 51 | + |
| 52 | + case 'm': |
| 53 | + case 'mountPath': |
| 54 | + $mountPath = $value; |
| 55 | + break; |
| 56 | + |
| 57 | + case 'l': |
| 58 | + case 'log': |
| 59 | + $logFileArg = $value; |
| 60 | + break; |
| 61 | + |
| 62 | + case 'd': |
| 63 | + case 'debug': |
| 64 | + $debug = TRUE; |
| 65 | + break; |
| 66 | + |
| 67 | + } |
| 68 | +} |
| 69 | +print_r($opts); |
| 70 | + |
| 71 | + |
| 72 | +if (!$host || !$sharesArray || !$credentials || !$mountPath) { |
| 73 | + die("The script is used like so: -h=host -s=directory1,directory2 -m=/path/mount/to -c=/path/to/cred [-p=port] [-l=/path/to/logfile]" . PHP_EOL); |
| 74 | +} |
| 75 | + |
| 76 | + |
| 77 | +/** |
| 78 | + * Code |
| 79 | + */ |
| 80 | +// By default script will log info into /dev/stdout. |
| 81 | +// Unless log file could be specified as second argument |
| 82 | +// or environment variable. |
| 83 | +$log = empty($logFile) ? NULL : fopen($logFile, "a+"); |
| 84 | + |
| 85 | +writeToLog("----"); // Just to more easily read logs |
| 86 | +echo writeToLog("Initiated check."); |
| 87 | + |
| 88 | +// Check if connection exists |
| 89 | +if (checkIfConnectionExists($host, $port)) { |
| 90 | + // If connection exists, check if shares are mounted, and if not, mount them. |
| 91 | + mountshares($host, $port, $sharesArray, $mountPath, $credentials); |
| 92 | + checkLastFile($mountPath, $sharesArray); |
| 93 | +} |
| 94 | +else { |
| 95 | + // in exit status - Also making sure the log file is closed properly. |
| 96 | + if ($log) { |
| 97 | + fclose($log); |
| 98 | + } |
| 99 | + exit("ERROR: Connection to $host through port $port is closed or not otherwise not available. Stopping script"); |
| 100 | +} |
| 101 | + |
| 102 | +echo writeToLog("Script has now finished the run"); |
| 103 | + |
| 104 | +// Close opened file. |
| 105 | +if ($log) { |
| 106 | + fclose($log); |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Functions |
| 111 | + */ |
| 112 | + |
| 113 | +/** |
| 114 | + * Simply checks if the connection to the host to the specified port is |
| 115 | + * accessible Currently does not send mails nor POST. |
| 116 | + * |
| 117 | + * @param $host |
| 118 | + * @param $port |
| 119 | + * @param $openLogfile |
| 120 | + * |
| 121 | + * @return bool |
| 122 | + */ |
| 123 | +function checkIfConnectionExists($host, $port = 445) { |
| 124 | + // Check if connection to host through port 445 is open |
| 125 | + $connectionOpen = @fsockopen($host, $port, $errNo, $errStr, 5); |
| 126 | + // If resource is available |
| 127 | + if (is_resource($connectionOpen)) { |
| 128 | + // Write to log |
| 129 | + echo writeToLog("Connection to $host through port $port is open. Continuing."); |
| 130 | + } |
| 131 | + else { |
| 132 | + // else, report error |
| 133 | + // in log |
| 134 | + echo writeToLog("ERROR: Connection to $host through port $port is closed or not otherwise not available. Stopping script"); |
| 135 | + |
| 136 | + } |
| 137 | + return TRUE; // If everything works, it'll return true. If nothing works, it'll exit anyway. |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * |
| 142 | + * Checks if shares are mounted, and if not mounts them. |
| 143 | + * Currently doesn't report any errors, because I don't know what an erroneous |
| 144 | + * output looks like at the moment. |
| 145 | + * |
| 146 | + * @param $host |
| 147 | + * @param $port |
| 148 | + * @param $sharesArray |
| 149 | + * @param $mountPath |
| 150 | + * @param $credentialFile |
| 151 | + * |
| 152 | + * @return bool |
| 153 | + */ |
| 154 | +function mountShares($host, $port, $sharesArray, $mountPath, $credentialFile) { |
| 155 | + global $debug; |
| 156 | + |
| 157 | + // Loop through shares |
| 158 | + foreach ($sharesArray as $share) { |
| 159 | + |
| 160 | + // Check if mount points are of type nfs or cifs |
| 161 | + $command = "df -P -T $mountPath/$share | tail -n +2 | awk '{print $2}'"; |
| 162 | + if ($debug) { |
| 163 | + echo writeToLog("DEBUG: Executing command: " . $command); |
| 164 | + } |
| 165 | + $checkMountPoint = exec($command); |
| 166 | + // If the mount point is of type nfs or cifs, don't remount it, and simply write to log that it's already mounted |
| 167 | + if ($checkMountPoint == "cifs") { |
| 168 | + echo writeToLog("The mount point $mountPath/$share is of type NFS or CIFS, so it's most likely mounted already. Not remounting."); |
| 169 | + } |
| 170 | + else { |
| 171 | + // If the mount point is of something else, mount the remote share to the mount points |
| 172 | + echo writeToLog("The mount point $mountPath/$share is not of type NFS or CIFS, so it's most likely not mounted. Mounting."); |
| 173 | + $command = "mount -t cifs //$host/$share $mountPath/$share -o cred=$credentialFile"; |
| 174 | + if ($debug) { |
| 175 | + echo writeToLog("DEBUG: Executing command: " . $command); |
| 176 | + } |
| 177 | + if ($port) { |
| 178 | + $command .= ',port=' . $port; |
| 179 | + } |
| 180 | + exec($command, $mountOutput); |
| 181 | + echo writeToLog(print_r($mountOutput, 1)); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return TRUE; |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * This simply checks the last modified file for each share |
| 190 | + * |
| 191 | + * @param $mountPath |
| 192 | + * @param $sharesArray |
| 193 | + * |
| 194 | + * @return bool |
| 195 | + */ |
| 196 | +function checkLastFile($mountPath, $sharesArray) { |
| 197 | + |
| 198 | + $findOutput = FALSE; // Just so we don't have an undefined variable |
| 199 | + |
| 200 | + foreach ($sharesArray as $share) { |
| 201 | + $findOutput = exec("/usr/bin/find $mountPath/$share -type f -printf '%T+\t%p\n' | /usr/bin/sort | /usr/bin/tail -1"); |
| 202 | + echo writeToLog("The last modified file in $mountPath/$share is: $findOutput"); |
| 203 | + } |
| 204 | + |
| 205 | + return $findOutput; |
| 206 | +} |
| 207 | + |
| 208 | +/** |
| 209 | + * This function helps put stuff in the configured log file |
| 210 | + * |
| 211 | + * @param $logString |
| 212 | + * |
| 213 | + * @return string |
| 214 | + */ |
| 215 | +function writeToLog($logString) { |
| 216 | + // Get current date and time. |
| 217 | + $dt = new DateTime(); |
| 218 | + $now = $dt->format('Y-m-d H:i:s'); |
| 219 | + |
| 220 | + // Attach date to logString. |
| 221 | + $dateString = "[" . $now . "] - "; |
| 222 | + $logString = $dateString . $logString . "\n"; |
| 223 | + |
| 224 | + global $log; |
| 225 | + // Do some logging. |
| 226 | + if (isset($log)) { |
| 227 | + fwrite($log, $logString); |
| 228 | + } |
| 229 | + return $logString; |
| 230 | +} |
0 commit comments