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:image001.png at 01D101D9.AE813790] AccuWeather 385 Science Park Road, State College, PA 16803 P: 814.235.8650 E: Foster.Patch at accuweather.com<mailto:Foster.Patch at accuweather.com> E: networkoperations at accuweather.com<mailto:networkoperations at accuweather.com> http://www.AccuWeather.com<http://www.accuweather.com/>
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 <Foster.Patch at accuweather.com> 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 385 Science Park Road, State College, PA 16803 P: 814.235.8650 E: *Foster.Patch at accuweather.com* <Foster.Patch at accuweather.com> E: *networkoperations at accuweather.com* <networkoperations at accuweather.com> *http://www.AccuWeather.com* <http://www.accuweather.com/>
Xymon mailing list Xymon at xymon.com http://lists.xymon.com/mailman/listinfo/xymon
participants (2)
-
Foster.Patch@accuweather.com
-
jlaidman@rebel-it.com.au