Creating Java Monitor for Xymon
list Foster Patch
Hello, I have created a Java Program that will pull a date from a stored procedure in a SQL Server, which then is compared to the current date, and will alarm red if it is past the threshold. Thus far, I have developed the code independent of Xymon, but now plan on implementing it since it is functioning properly. Below is my java code. Instead of printing out "Uh oh" if the date is older than 2 hours, I would like to make it alarm on the servers page. I don't know where to begin from here, and any suggestions or pointers in the right direction would be great. Additionally, any examples of using Java as a monitor in Xymon would be very beneficial. I plan on storing this in \ext\trunk\monitor.d\120.
package connect;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Sqlselection {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
CallableStatement callableStatement = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection("jdbc:sqlserver://sqldev-v02.accu.accuwx.com;database=analysis_summary;integratedSecurity=true;");
String getCall = "{call proc_Return_Latest_AirQuality_DateTime}";
callableStatement = conn.prepareCall(getCall);
ResultSet rs = callableStatement.executeQuery();
while (rs.next()) {
String delims = "[.]+";
String seconddelim = "[- :]+";
String columnValue = rs.getString(1);
String[] tokens = columnValue.split(delims);
String checkTime[] = tokens[0].split(seconddelim);
int year = Integer.parseInt(checkTime[0]);
int month = Integer.parseInt(checkTime[1]);
int day = Integer.parseInt(checkTime[2]);
int hour = Integer.parseInt(checkTime[3]);
int minute = Integer.parseInt(checkTime[4]);
int second = Integer.parseInt(checkTime[5]);
Calendar cal = Calendar.getInstance();
Date todayDate = new Date();
todayDate = cal.getTime();
Date checkDate = new Date();
checkDate.setYear(year-1900);
checkDate.setMonth(month);
checkDate.setDate(day);
checkDate.setHours(hour);
checkDate.setMinutes(minute);
checkDate.setSeconds(second);
todayDate.setHours(todayDate.getHours()-2);
if(checkDate.before(todayDate)){
System.out.println("UH OH");
}
else if(checkDate.after(todayDate)){
System.out.println("yep");
}
}
}
}
Foster Patch
Web Server Technician
[cid:user-07cdeafb24d6@xymon.invalid813790] AccuWeather
XXX Science Park Road, State College, PA XXXXX
P: XXX.XXX.XXXX
E: user-830dc635f051@xymon.invalid<mailto:user-830dc635f051@xymon.invalid>
E: user-d05d8627427f@xymon.invalid<mailto:user-d05d8627427f@xymon.invalid>
http://www.AccuWeather.com<http://www.accuweather.com/>
list Jeremy Laidman
Foster On what OS are you running your Java monitor? I can think of a few different ways to do this type of thing. Firstly, you could use the Java Socket() function to connect to the Xymon server(s) and send a "BB" format message. Secondly, you could use some kind of wrapper script or batch file that runs the Java program, and then performs the socket connection to send the raw "BB" message. Thirdly, you could use a wrapper or batch file to run the Java program, and then run a Xymon client program to send the message. Cheers Jeremy On Thu, Feb 18, 2016 at 7:00 AM Foster Patch <user-830dc635f051@xymon.invalid>
▸
wrote:
Hello, I have created a Java Program that will pull a date from a stored
procedure in a SQL Server, which then is compared to the current date, and
will alarm red if it is past the threshold. Thus far, I have developed the
code independent of Xymon, but now plan on implementing it since it is
functioning properly. Below is my java code. Instead of printing out “Uh
oh” if the date is older than 2 hours, I would like to make it alarm on the
servers page. I don’t know where to begin from here, and any suggestions or
pointers in the right direction would be great. Additionally, any examples
of using Java as a monitor in Xymon would be very beneficial. I plan on
storing this in \ext\trunk\monitor.d\120.
*package* connect;
*import* java.sql.DriverManager;
*import* java.sql.CallableStatement;
*import* java.sql.Connection;
*import* java.sql.ResultSet;
*import* java.sql.SQLException;
*import* *java.text.DateFormat*;
*import* *java.text.SimpleDateFormat*;
*import* java.util.Calendar;
*import* java.util.Date;
*public* *class* Sqlselection {
*public* *static* *void* main(String[] args) *throws*
SQLException, ClassNotFoundException {
CallableStatement callableStatement = *null*;
Class.*forName*(
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.*getConnection*(
"jdbc:sqlserver://sqldev-v02.accu.accuwx.com
;database=analysis_summary;integratedSecurity=true;");
▸
String getCall = "{call
proc_Return_Latest_AirQuality_DateTime}";
callableStatement = conn.prepareCall(getCall);
ResultSet rs = callableStatement.executeQuery();
*while* (rs.next()) {
String delims = "[.]+";
String seconddelim = "[- :]+";
String columnValue = rs.getString(1);
String[] tokens = columnValue.split(delims
);
String checkTime[] = tokens[0].split(
seconddelim);
*int* year = Integer.*parseInt*(checkTime
[0]);
*int* month = Integer.*parseInt*(checkTime
[1]);
*int* day = Integer.*parseInt*(checkTime
[2]);
*int* hour = Integer.*parseInt*(checkTime
[3]);
*int* minute = Integer.*parseInt*(
checkTime[4]);
*int* second = Integer.*parseInt*(
checkTime[5]);
▸
Calendar cal = Calendar.*getInstance*();
Date todayDate = *new* Date();
todayDate = cal.getTime();
Date checkDate = *new* Date();
checkDate.*setYear**(**year**-1900)*;
checkDate.*setMonth**(**month**)*;
checkDate.*setDate**(**day**)*;
checkDate.*setHours**(**hour**)*;
checkDate.*setMinutes**(**minute**)*;
checkDate.*setSeconds**(**second**)*;
todayDate.*setHours**(**todayDate**.*
*getHours**()-2)*;
▸
*if*(checkDate.before(todayDate)){
System.*out*.println("UH OH");
}
*else* *if*(checkDate.after(todayDate)){
System.*out*.println("yep");
}
}
}
}
Foster Patch
Web Server Technician
AccuWeather
XXX Science Park Road, State College, PA XXXXX
P: XXX.XXX.XXXXE: *user-830dc635f051@xymon.invalid* <user-830dc635f051@xymon.invalid> E: *user-d05d8627427f@xymon.invalid* <user-d05d8627427f@xymon.invalid> *http://www.AccuWeather.com* <http://www.accuweather.com/>;