// // checkFileAge.java // nagios plugin to monitor directories for files older than a given # of milliseconds // // Author: Sergei Haramundanis // Date: 27-Apr-2006 // Update: 17-Jan-2007 by S. Haramundanis to include support for check_from_time and check_to_time // Update: [1.4] 22-May-2007 by S. Haramundanis to report target_file_age_ms in human readable format // // usage: java checkFileAge // // Description: // // This plugin will scan a list of directories contained in the data file specified to determine if any files // in those directories have not been modified for longer than the specified target file age in milliseconds // // Lines beginning with # or blank lines in the data file are ignored. // // Output: // // During any run of the plugin, if it finds any files older than the specified number of milliseconds // it will return a WARNING state with the last file found prefixed by the number of files that were found // to be older as in the following example: // // [WARNING] [5] "/ftp/inbound/filename.pdf" is older than 1800000 ms // // in this case the [5] indicates that it found five files older than 30 minutes (1800000 ms) and // filename.pdf is the last one it found // // if no files are found to be older in the directory list it will return an OK state with the message: // // [OK] no files found older than ms // // If the current time falls outside the specified check_from_time and check_to_time it will return on OK state with the message: // // [OK] current time outside of monitoring timeframe 1600 and 1800 // // import java.util.*; import java.io.*; import java.text.*; public class checkFileAge { static String productVersion = "checkFileAge 1.4"; // nagios return codes: static int STATE_OK = 0; static int STATE_WARNING = 1; static int STATE_CRITICAL = 2; static int STATE_UNKNOWN = 3; static int STATE_DEPENDENT = 4; static String str_data_filename = ""; static long l_target_file_age_ms = 0; static String str_check_from_time = ""; static String str_check_to_time = ""; static Vector v_monitor_directory = new Vector(); static String str_last_old_file_message = ""; public static void main(String[] args) { if ((args.length == 0) || (args[0].equals("--help"))) { System.out.println(productVersion); System.out.println(""); System.out.println("nagios plugin to monitor directories for files older than a given # of milliseconds"); System.out.println(""); System.out.println("This nagios plugin comes with ABSOLUTELY NO WARRANTY."); System.out.println("You may redistribute copies of this plugin under the terms of the GNU General Public License"); System.out.println("as long as the original author, edit history and description information remain in place."); System.out.println(""); System.out.println("usage: java checkFileAge "); System.out.println("usage: java checkFileAge --help"); System.out.println("usage: java checkFileAge --version"); System.exit(STATE_OK); } else if (args[0].equals("--version")) { System.out.println(productVersion); System.out.println("This nagios plugin comes with ABSOLUTELY NO WARRANTY."); System.out.println("You may redistribute copies of this plugin under the terms of the GNU General Public License"); System.out.println("as long as the original author, edit history and description information remain in place."); System.exit(STATE_OK); } else if (args.length != 4) { System.out.println("usage: java checkFileAge "); System.exit(STATE_CRITICAL); } else { long startTime = System.currentTimeMillis(); str_data_filename = args[0].trim(); // determine if data file exists File file_data_file = new File(str_data_filename); if (!file_data_file.exists() || !file_data_file.isFile()) { System.out.println("data file \""+str_data_filename+"\" does not exist"); System.exit(STATE_CRITICAL); } // strip non-ascii characters String str_target_file_age_ms = ""; byte[] bytearray_target_file_age_ms = args[1].trim().getBytes(); for (int i=0; i < bytearray_target_file_age_ms.length; i++) { byte temp_byte = bytearray_target_file_age_ms[i]; if ( (temp_byte >= 48) && (temp_byte <= 57) ) { str_target_file_age_ms += new String(bytearray_target_file_age_ms, i, 1); } } //System.out.println("str_target_file_age_ms = \""+str_target_file_age_ms+"\""); // determine if target file age ms is a number if ((isNotANumber(str_target_file_age_ms)) || (str_target_file_age_ms.indexOf(".") >= 0) || (str_target_file_age_ms.indexOf("-") >= 0) || (str_target_file_age_ms.indexOf("+") >= 0)) { System.out.println("target file age ms \""+str_target_file_age_ms+"\" is not a valid long number"); System.exit(STATE_CRITICAL); } // set target file age ms l_target_file_age_ms = new Long(str_target_file_age_ms).longValue(); // determine if check_from_time is valid // strip non-ascii characters byte[] bytearray_check_from_time = args[2].trim().getBytes(); for (int i=0; i < bytearray_check_from_time.length; i++) { byte temp_byte = bytearray_check_from_time[i]; if ( (temp_byte >= 48) && (temp_byte <= 57) ) { str_check_from_time += new String(bytearray_check_from_time, i, 1); } } if ((isNotANumber(str_check_from_time)) || (str_check_from_time.length() != 4) || (str_check_from_time.indexOf(".") >= 0) || (str_check_from_time.indexOf("-") >= 0) || (str_check_from_time.indexOf("+") >= 0)) { System.out.println("check from time \""+str_check_from_time+"\" is not valid: must be in format ####"); System.exit(STATE_CRITICAL); } int int_check_from_time = Integer.parseInt(str_check_from_time); if ( (int_check_from_time < 0) || (int_check_from_time > 2359) ) { System.out.println("check from time \""+str_check_from_time+"\" is not valid: must be between 0000 and 2359"); System.exit(STATE_CRITICAL); } // determine if check_to_time is valid // strip non-ascii characters byte[] bytearray_check_to_time = args[3].trim().getBytes(); for (int i=0; i < bytearray_check_to_time.length; i++) { byte temp_byte = bytearray_check_to_time[i]; if ( (temp_byte >= 48) && (temp_byte <= 57) ) { str_check_to_time += new String(bytearray_check_to_time, i, 1); } } if ((isNotANumber(str_check_to_time)) || (str_check_to_time.length() != 4) || (str_check_to_time.indexOf(".") >= 0) || (str_check_to_time.indexOf("-") >= 0) || (str_check_to_time.indexOf("+") >= 0)) { System.out.println("check to time \""+str_check_to_time+"\" is not valid: must be in format ####"); System.exit(STATE_CRITICAL); } int int_check_to_time = Integer.parseInt(str_check_to_time); if ( (int_check_to_time < 0) || (int_check_to_time > 2359) ) { System.out.println("check to time \""+str_check_to_time+"\" is not valid: must be between 0000 and 2359"); System.exit(STATE_CRITICAL); } if (int_check_from_time >= int_check_to_time) { System.out.println("check_from_time \""+str_check_from_time+"\" must be less than check_to_time \""+str_check_to_time+"\""); System.exit(STATE_CRITICAL); } // load directories to check in data file // ignore comments and blank lines RandomAccessFile raf_data_file = null; try { raf_data_file = new RandomAccessFile(str_data_filename, "r"); } catch (java.io.FileNotFoundException exception) { System.out.println("java.io.FileNotFoundException encountered attempting to open data file \""+str_data_filename+"\" : "+exception); System.exit(STATE_CRITICAL); } catch (java.io.IOException exception) { System.out.println("java.io.IOException encountered attempting to open data file \""+str_data_filename+"\" : "+exception); System.exit(STATE_CRITICAL); } String str_record = ""; while (str_record != null) { try { str_record = raf_data_file.readLine(); } catch (java.io.IOException exception) { System.out.println("java.io.IOException encountered attempting to read data file \""+str_data_filename+"\" : "+exception); System.exit(STATE_CRITICAL); } if ((str_record != null) && (!str_record.trim().equals("")) && (!str_record.trim().startsWith("#"))) { v_monitor_directory.add(str_record.trim()); } } try { raf_data_file.close(); } catch (java.io.IOException exception) { System.out.println("java.io.IOException encountered attempting to close data file \""+str_data_filename+"\" : "+exception); System.exit(STATE_CRITICAL); } // check if current time is within check_from_time and check_to_time Calendar currentCalendar = Calendar.getInstance(); int int_current_hour = currentCalendar.get(Calendar.HOUR_OF_DAY); int int_current_minute = currentCalendar.get(Calendar.MINUTE); String str_current_hour = ""; String str_current_minute = ""; if (int_current_hour < 10) { str_current_hour = "0"+Integer.toString(int_current_hour); } else { str_current_hour = Integer.toString(int_current_hour); } if (int_current_minute < 10) { str_current_minute = "0"+Integer.toString(int_current_minute); } else { str_current_minute = Integer.toString(int_current_minute); } String str_current_time = str_current_hour+str_current_minute; int int_current_time = Integer.parseInt(str_current_time); if ( (int_current_time >= int_check_from_time) && (int_current_time <= int_check_to_time) ) { int return_val = monitorDirectories(); if (return_val > 0) { long endTime = System.currentTimeMillis(); long elapsedTimeMillisecs = (endTime-startTime); System.out.println("[WARNING] "+str_last_old_file_message+" | elapsedTimeMillisecs="+(elapsedTimeMillisecs)); System.exit(STATE_WARNING); } else { long endTime = System.currentTimeMillis(); long elapsedTimeMillisecs = (endTime-startTime); System.out.println("[OK] no files found older than "+targetAgeHumanReadable()+" | elapsedTimeMillisecs="+(elapsedTimeMillisecs)); System.exit(STATE_OK); } } else { long endTime = System.currentTimeMillis(); long elapsedTimeMillisecs = (endTime-startTime); System.out.println("[OK] current time outside of monitoring timeframe "+str_check_from_time+" and "+str_check_to_time+" | elapsedTimeMillisecs="+(elapsedTimeMillisecs)); System.exit(STATE_OK); } } }// end public static void main(String[] args) public static int monitorDirectories() { int int_old_files_found_count = 0; try { for (int i=0; i < v_monitor_directory.size(); i++) { String directoryName = (String)v_monitor_directory.elementAt(i); File file_directory = new File(directoryName); if (!file_directory.exists()) { System.out.println("directory \""+directoryName+"\" does not exist"); System.exit(STATE_CRITICAL); } else if (!file_directory.isDirectory()) { System.out.println("directory \""+directoryName+"\" is not a directory"); System.exit(STATE_CRITICAL); } File[] directoryList = file_directory.listFiles(); if (directoryList == null) { System.out.println("problem accessing directory \""+directoryName+"\"; check permissions"); System.exit(STATE_CRITICAL); } for (int file_index=0; file_index < directoryList.length; file_index++) { if (directoryList[file_index].isFile()) { // if this file hasn't been modified for report it long long_current_time = System.currentTimeMillis(); if ((long_current_time - directoryList[file_index].lastModified()) > l_target_file_age_ms) { int_old_files_found_count++; str_last_old_file_message = "["+int_old_files_found_count+"] \""+directoryList[file_index]+"\" is older than "+targetAgeHumanReadable(); } } } } } catch (Exception exception) { System.out.println("Exception encountered during monitorDirectories(): "+exception); System.exit(STATE_CRITICAL); } return int_old_files_found_count; }// end public int monitorDirectories() public static boolean isNotANumber(String value) { // check if the string value is convertable to a number ParsePosition parsePosition = new ParsePosition(0); Object numberObject = NumberFormat.getInstance().parseObject(value, parsePosition); if ( (parsePosition.getIndex() != value.length()) || (numberObject == null) ) { // not a number! return true; } else { return false; } }// end public boolean isNotANumber(String value) public static String targetAgeHumanReadable() { long l_target_age_ms = l_target_file_age_ms; String str_return_val = ""; // convert l_target_age_ms to format: weeks, days, hours, minutes, seconds, milliseconds // 1000 milliseconds per second // 60 seconds per minute // 3600 seconds per hour // 86400 seconds per day // 604800 seconds per week long weeks = 0; long days = 0; long hours = 0; long minutes = 0; long seconds = 0; long milliseconds = 0; while (l_target_age_ms/1000 >= 604800) { weeks++; l_target_age_ms = l_target_age_ms - (604800*1000); } while (l_target_age_ms/1000 >= 86400) { days++; l_target_age_ms = l_target_age_ms - (86400*1000); } while (l_target_age_ms/1000 >= 3600) { hours++; l_target_age_ms = l_target_age_ms - (3600*1000); } while (l_target_age_ms/1000 >= 60) { minutes++; l_target_age_ms = l_target_age_ms - (60*1000); } while (l_target_age_ms/1000 >= 1) { seconds++; l_target_age_ms = l_target_age_ms - (1*1000); } if (weeks > 0) { if (weeks == 1) { str_return_val += weeks+" week "; } else { str_return_val += weeks+" weeks "; } } if (days > 0) { if (days == 1) { str_return_val += days+" day "; } else { str_return_val += days+" days "; } } if (hours > 0) { if (hours == 1) { str_return_val += hours+" hour "; } else { str_return_val += hours+" hours "; } } if (minutes > 0) { if (minutes == 1) { str_return_val += minutes+" minute "; } else { str_return_val += minutes+" minutes "; } } if (seconds > 0) { if (seconds == 1) { str_return_val += seconds+" second "; } else { str_return_val += seconds+" seconds "; } } if (l_target_age_ms > 0) { if (l_target_age_ms == 1) { str_return_val += l_target_age_ms+" millisecond"; } else { str_return_val += l_target_age_ms+" milliseconds"; } } return str_return_val; }// end public static String targetAgeHumanReadable() }// end public class checkFileAge