Hallo allemaal!
Ik ben druk bezig met een MSSQL Job Monitor aan het maken door middel van een unix shell script en een java source. Het lijkt erop dat mijn java source volledig werkt behalve als de naam van een mssql job een spatie bevat. Hert script is bedoeld voor een monitor s ysteem genaamt groundwork vandaar deze manier van programmeren.
De inhoud van het .sh script:
#!/bin/sh
PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
#. $PROGPATH/utils.sh
javacmd=/usr/java/jdk1.6.0_22/bin/java
CLASSPATH=/usr/local/groundwork/nagios/libexec:/usr/local/groundwork/nagios/libexec/sqljdbc.jar
export CLASSPATH
. /usr/local/groundwork/nagios/libexec/utils.sh
argc=$#
print_usage() {
echo "Usage: $PROGNAME -H hostaddress -u {username} -p {password}"
}
print_help() {
print_usage
echo ""
echo "-h for help"
echo ""
echo "-H (hostaddress)"
echo "-u (username)"
echo "-p (password)"
echo "Example command"
echo "check_sqljob -H 10.70.5.45 -u sa -p s22css"
echo ""
exit 0
}
case "$1" in
-h)
print_help
exit 0
;;
-v)
print_revision $PROGNAME $REVISION
exit 0
;;
-H)
if [ $argc -eq 8 ]
then
ret=`$javacmd SqlJobMon $2 $4 $6 $7 $8`
fi
ret1=`echo $ret | grep CRITICAL| awk '{print $4}'`
if [ "$ret1" = "CRITICAL:" ]
then
echo $ret
exit 2
fi
ret2=`echo $ret | grep OK| awk '{print $4}'`
if [ "$ret2" = "OK:" ]
then
echo $ret
exit 0
else
echo "Unknown:"
echo "ret: $ret ret1: $ret1 ret2 $ret2"
echo "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6 7: $7 8: $8 9: $9 10: $10"
exit 3
fi
;;
*)
print_help
exit $STATE_UNKNOWN
;;
esac
De inhoud van de java source:
Het probleem zit hem dus op het moment dat ik een argument(args) opgeef met een spatie erin.
Het uitvoeren van de java source gaat alsvolgt:
java SqlJobMon <hostname> <username> <password> <jobname>
en bij de jobname gaat het dus fout ik heb al geprobeerd om " " om de naam te zetten en ' ' maar hij pakt het nogsteeds niet goed op in de java source.
Wie o wie kan mij helpen?
Alvast bedankt!
- M34nM4chin3
Ik ben druk bezig met een MSSQL Job Monitor aan het maken door middel van een unix shell script en een java source. Het lijkt erop dat mijn java source volledig werkt behalve als de naam van een mssql job een spatie bevat. Hert script is bedoeld voor een monitor s ysteem genaamt groundwork vandaar deze manier van programmeren.
De inhoud van het .sh script:
#!/bin/sh
PROGNAME=`basename $0`
PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
#. $PROGPATH/utils.sh
javacmd=/usr/java/jdk1.6.0_22/bin/java
CLASSPATH=/usr/local/groundwork/nagios/libexec:/usr/local/groundwork/nagios/libexec/sqljdbc.jar
export CLASSPATH
. /usr/local/groundwork/nagios/libexec/utils.sh
argc=$#
print_usage() {
echo "Usage: $PROGNAME -H hostaddress -u {username} -p {password}"
}
print_help() {
print_usage
echo ""
echo "-h for help"
echo ""
echo "-H (hostaddress)"
echo "-u (username)"
echo "-p (password)"
echo "Example command"
echo "check_sqljob -H 10.70.5.45 -u sa -p s22css"
echo ""
exit 0
}
case "$1" in
-h)
print_help
exit 0
;;
-v)
print_revision $PROGNAME $REVISION
exit 0
;;
-H)
if [ $argc -eq 8 ]
then
ret=`$javacmd SqlJobMon $2 $4 $6 $7 $8`
fi
ret1=`echo $ret | grep CRITICAL| awk '{print $4}'`
if [ "$ret1" = "CRITICAL:" ]
then
echo $ret
exit 2
fi
ret2=`echo $ret | grep OK| awk '{print $4}'`
if [ "$ret2" = "OK:" ]
then
echo $ret
exit 0
else
echo "Unknown:"
echo "ret: $ret ret1: $ret1 ret2 $ret2"
echo "1: $1 2: $2 3: $3 4: $4 5: $5 6: $6 7: $7 8: $8 9: $9 10: $10"
exit 3
fi
;;
*)
print_help
exit $STATE_UNKNOWN
;;
esac
De inhoud van de java source:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
| //===================================================================== // // File: SqlJonMon.java // Summary: NAGIOS plugin to determine recent and history SQL Server Agent Job failures // current date. // Date: July 2010 // //===================================================================== import java.sql.*; import java.io.*; import java.util.Date; import java.util.Properties; import java.text.DateFormat; import java.text.SimpleDateFormat; public class SqlJobMon { public static void main(String[] args) { int count = 0; String connectionUrl; String datetime; String datequery; String jobname; String test; DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); DateFormat dateQuery = new SimpleDateFormat("yyyyMMdd"); Date date = new Date(); FileOutputStream out = null; // declare a file output object PrintStream p = null; // declare a print stream object datetime = dateFormat.format(date); datequery = dateQuery.format(date); test = "JOB 1"; String style = "<style type=\"text/css\"> #gradient-style" + "{" + " font-family: \"Lucida Sans Unicode\", \"Lucida Grande\", Sans-Serif;" + "font-size: 12px;" + "margin: 0px;" + "width: 1000px;" + "text-align: left;" + "border-collapse: collapse;" + "}" + "#gradient-style th" + "{" + "font-size: 13px;" + "font-weight: normal;" + "padding: 8px;" + "background: #b9c9fe;" + "border-top: 2px solid #d3ddff;" + "border-bottom: 1px solid #fff;" + "color: #039;" + "}" + "#gradient-style td" + "{" + "padding: 8px;" + "border-bottom: 1px solid #fff;" + "color: #669;" + "border-top: 1px solid #fff;" + "background: #e8edff;" + "}" + "#gradient-style tfoot tr td" + "{" + "background: #e8edff;" + "font-size: 12px;" + "color: #99c;" + "}" + "#gradient-style tbody tr:hover td" + "{" + "background: #d0dafd;" + "color: #339;" + "}" + "#gradient-style caption" + "{" + "font-family: \"Lucida Sans Unicode\", \"Lucida Grande\", Sans-Serif; font-weight: bold; font-size: 13px;color: #039;" + "}</style>"; connectionUrl = "jdbc:sqlserver://" + args[0] + ":1433;databaseName=msdb;integratedSecurity=false;user=" + args[1] + ";password=" + args[2]; // Declare the JDBC objects. Connection con = null; Statement stmt = null; ResultSet rsHistory = null, rsRecent = null; try { // Reading values from property file String link = new SqlJobMon().readPropertiesFile("HTML_LINK"); String currentDirectory = new SqlJobMon() .readPropertiesFile("HTML_DIR"); File file = new File(currentDirectory + "sqllog"); boolean exist = file.exists(); if (!exist) { boolean success = (new File(currentDirectory + "sqllog")) .mkdir(); } out = new FileOutputStream(currentDirectory + "sqllog/" + args[0] + "-" + datetime + ".html"); p = new PrintStream(out); // Establish the connection. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager.getConnection(connectionUrl); // Create and execute an SQL statement For getting history and // recent job failed. String sqlHistory = "select sj.name, sjh.step_name, sjh.run_date, sjh.run_time, sjh.run_status, sjh.message, sjh.server " + "from msdb.dbo.sysjobhistory sjh inner join msdb.dbo.sysjobs sj on sjh.job_id = sj.job_id " + "inner join msdb.dbo.sysjobsteps sjs on sj.job_id = sjs.job_id and sjh.step_id = sjs.step_id " + "where sjh.run_status = 0" + "and sj.name ='" + args[3] + "'"; stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rsHistory = stmt.executeQuery(sqlHistory); String sqlRecent = "select top 1 sj.name, sjh.step_name, sjh.run_date, sjh.run_time, sjh.run_status, sjh.message, sjh.server " + "from msdb.dbo.sysjobhistory sjh inner join msdb.dbo.sysjobs sj on sjh.job_id = sj.job_id " + "inner join msdb.dbo.sysjobsteps sjs on sj.job_id = sjs.job_id and sjh.step_id = sjs.step_id " + "and sj.name ='" + test + "'" + "and sjh.run_date ='" + datequery + "'" + "ORDER BY run_time DESC"; stmt = null; stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rsRecent = stmt.executeQuery(sqlRecent); while (rsRecent.next()) { if (rsRecent.getString("run_status").equals("0")){ rsRecent.beforeFirst(); p .println("<html>\n<head>" + style + "<title>Failed SQL Job </title>" + "</head><table border=1 id=\"gradient-style\">\n<CAPTION>Recent SQL Job Failed For " + args[3] + datetime + "</CAPTION>\n" + "<th>Date</th>\n<th>Time</th>\n<th>Server</th>\n<th>Job_Name</th>\n<th>Step_Name</th>\n<th>Run_Status" + "</th>\n<th>Message</th>\n"); // Iterate through the data in the result set and display it. while (rsRecent.next()) { p.println("<tr>"); p.print("<td>"); p.print(rsRecent.getString("run_date")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("run_time")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("server")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("name")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("step_name")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("run_status")); p.print("</td>\n"); p.print("<td>"); p.print(rsRecent.getString("message")); p.print("</td>\n"); p.println("</tr>"); } p.println("</table>"); p.println("<a href=JavaScript:history.back();>[ back ]</a>"); System.out.print("<a href=" + link + "/" + args[0] + "-" + datetime + ".html target=main>"); System.out.print(" CRITICAL: Job Failed" ); System.out.print("</a>\n"); } else { if (rsRecent.getString("run_status").equals("1")){ rsRecent.beforeFirst(); p .println("<html>\n<head>" + style + "<title>Failed SQL Job </title>" + "</head><table border=1 id=\"gradient-style\">\n<CAPTION>Recent SQL Job Failed For " + args[3] + datetime + "</CAPTION>\n" + "<th>Date</th>\n<th>Time</th>\n<th>Server</th>\n<th>Job_Name</th>\n<th>Step_Name</th>\n<th>Run_Status" + "</th>\n<th>Message</th>\n"); System.out.print("<a href=" + link + "/" + args[0] + "-" + datetime + ".html target=main>"); System.out.print(" OK: Nothing Failed"); System.out.print("</a>\n"); System.out.flush(); p.println("No Failed Jobs"); p.println("</table>"); p.println("<a href=JavaScript:history.back();>[ back ]</a>"); } } } } // Handle any errors that may have occurred. catch (Exception e) { e.printStackTrace(); } finally { if (out != null) try { p.close(); out.close(); } catch (Exception e) { } if (rsRecent != null) try { rsRecent.close(); } catch (Exception e) { } if (rsHistory != null) try { rsHistory.close(); } catch (Exception e) { } if (stmt != null) try { stmt.close(); } catch (Exception e) { } if (con != null) try { con.close(); } catch (Exception e) { } } } /* * To read the property file and return the value for key which is stored in * the property file */ public String readPropertiesFile(String key) { String value = null; try { String path = null; Properties prop = new Properties(); File dir1 = new File("."); /* To get the current working directory */ path = dir1.getCanonicalPath(); prop.load(new FileInputStream(path + "/url.properties")); value = prop.getProperty(key); } catch (Exception e) { e.printStackTrace(); } return value; } } |
Het probleem zit hem dus op het moment dat ik een argument(args) opgeef met een spatie erin.
Het uitvoeren van de java source gaat alsvolgt:
java SqlJobMon <hostname> <username> <password> <jobname>
en bij de jobname gaat het dus fout ik heb al geprobeerd om " " om de naam te zetten en ' ' maar hij pakt het nogsteeds niet goed op in de java source.
Wie o wie kan mij helpen?
Alvast bedankt!
- M34nM4chin3