-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcheck_dbversion_sybase.java
198 lines (176 loc) · 8.14 KB
/
check_dbversion_sybase.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
// ----------------------------------------------------------------------------
// check_dbversion_sybase.java 20140121 frank4dd version 1.0
// ----------------------------------------------------------------------------
// e-mail: support[at]frank4dd.com
// web: http://nagios.fm4dd.com/
//
// This nagios plugin queries the Sybase DB's @@version output.
// Example return string:
// Adaptive Server Enterprise/15.7/EBF 21338 SMP SP101 /P/NT (IX86)/Windows
// 2008 R2/ase157sp101/3439/32-bit/OPT/Thu Jun 06 12:02:54 2013
//
// Pre-requisites: Sybase JDBC driver installed and DB user has select rights.
// The jdbc driver is the free jTDS version.
// ----------------------------------------------------------------------------
// Example Output:
// > java check_dbversion_sybase 192.168.98.128 5000 mydb dbuser "password"
// Version OK: |
// ----------------------------------------------------------------------------
// return codes 'OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4
// ----------------------------------------------------------------------------
import java.sql.*;
import java.io.*;
import java.util.*;
class check_dbversion_sybase {
static int debug = 0; // 'normal'=>0,'verbose'=>1 when -d parameter is given
static String db_name = ""; // varchar(128)
static String release = ""; // varchar(128)
static String s_level = ""; // varchar(128)
static String b_level = ""; // varchar(128)
static String prdname = ""; // varchar(128)
static String version = "";
static String cfgfile = ""; // the returned tablespace value of space used in percent
static String[] cfgdata = new String[1000];
static String output = ""; // the plugin output string
static String perfdata = ""; // the plugin perfdata output, returning the KB values
static String dbUrl = ""; // the access URL for the database to query
static String query = ""; // the SQL query to execute
public static void main (String args[]) {
if (args.length < 5) {
System.err.println("Error: Missing Arguments.");
System.err.println("Usage: java check_dbversion_sybase <db-ip> <db-port> <db-instance> <db-user> <db-pwd> [-d]");
System.err.println("Usage: java check_dbversion_sybase <db-ip> <db-port> <db-instance> <db-user> <db-pwd> -f configfile");
System.exit(-1);
}
// Check if we got -d for debug
if (args.length == 6 && args[5].equals("-d")) { debug=1;}
// Check if we got a config file to compare against
if (args.length == 7 && args[5].equals("-f")) {
cfgfile=args[6];
try {
// Open the file
FileInputStream fstream = new FileInputStream(cfgfile);
// Convert our input stream to a DataInputStream
BufferedReader in = new BufferedReader(new InputStreamReader(fstream));
// Continue to read lines while there are still some left to read
int counter = 0;
while (in.ready()) {
String line = in.readLine();
line = line.trim();
if (! line.startsWith("#")) {
// load config data and ignore comments
cfgdata[counter] = line;
counter++;
}
}
in.close();
fstream.close();
}
catch (Exception e) { System.err.println("File input error"); }
}
dbUrl = "jdbc:jtds:sybase://" + args[0] +":" + args[1] +"/" + args[2] + ";user=" + args[3] + ";password=" + args[4] + ";";
if (debug == 1) { System.out.println("DB connect: " + dbUrl); }
try {
// use the JDBCtype 4 driver
Class.forName("net.sourceforge.jtds.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.err.println("Error: JDBC Driver Problem.");
System.err.println (e);
System.exit (3);
}
try {
// open connection to database "jdbc:jtds:sybase://localhost:5000/dbname;user=dbuser;password=dbpwd;"
Connection connection = DriverManager.getConnection(dbUrl);
// build query
query = "SELECT @@version";
if (debug == 1) { System.out.println ("DB query: " + query); }
// execute query
Statement statement = connection.createStatement ();
ResultSet rs = statement.executeQuery (query);
// get database information into performance data field
DatabaseMetaData dbmd = connection.getMetaData();
prdname = dbmd.getDatabaseProductName();
while ( rs.next () ) {
if (debug == 1) {
System.out.format ("debug: %40s", rs.getString(1)); // varchar(128)
}
// Decode the received version string, field separator is '/'
// Adaptive Server Enterprise/15.7/EBF 21338 SMP SP101 /P/NT (IX86)/Windows
// 2008 R2/ase157sp101/3439/32-bit/OPT/Thu Jun 06 12:02:54 2013
// Field order:
// 1. Product.
// 2. Version number.
// 3. Build number - this is a Sybase internal reference.
// 4. Release type: production (P), beta (B) or SWR version.
// 5. Platform identifier.
// 6. OS release under which the binary was compiled. (hard coded; not determined from running OS)
// 7. Codeline used for this release - a Sybase internal reference.
// 8. Build number - a Sybase internal reference.
// 9. 32-bit or 64-bit system indicator.
// 10. Type of post-build optimization server.
// 11. Compilation date and time.
// We select 1, 2, 7, 8
String delimiter = "/";
String[] version;
version = rs.getString(1).split(delimiter);
prdname = version[0];
release = version[1];
s_level = version[6];
b_level = version[7];
}
rs.close () ;
statement.close () ;
connection.close () ;
} catch (java.sql.SQLException e) {
System.err.println (e) ;
System.exit (3) ; // Unknown
}
version = prdname + " v" + release + " " + s_level;
output = version + ", " + b_level;
// If we have no config file, we are in reporting mode
if ( cfgfile.equals("") ) {
System.out.println("Version OK: " + output + "|" + perfdata);
System.exit (0); // OK
} else {
//################################################################################
//# We are in 'compliance' mode, we check the DB Version against the config file
//################################################################################
int counter=0;
String required = "";
String dbgroup = "";
String dbversion= "";
String remarks = "";
while(cfgdata[counter] != null) {
StringTokenizer st = new StringTokenizer(cfgdata[counter], "|");
if (st.hasMoreTokens()) { required = st.nextToken(); }
if (st.hasMoreTokens()) { dbgroup = st.nextToken(); }
if (st.hasMoreTokens()) { dbversion = st.nextToken(); }
if (st.hasMoreTokens()) { remarks = st.nextToken(); }
if( dbgroup.equals("sybase") && dbversion.equals(version) && required.equals("approved")) {
if(! remarks.equals("")) { perfdata = remarks; }
System.out.println("Version OK: " + version + "|" + perfdata);
System.exit (0); // OK
}
if( dbgroup.equals("sybase") && dbversion.equals(version) && required.equals("obsolete")) {
if(! remarks.equals("")) { perfdata = remarks; }
System.out.println("Version WARN: " + version + " obsolete" + "|" + perfdata);
System.exit (1); // WARN
}
if( dbgroup.equals("sybase") && dbversion.equals(version) && required.equals("med-vuln")) {
if(! remarks.equals("")) { perfdata = remarks; }
System.out.println("Version WARN: " + version + " vulnerable (low-medium)" + "|" + perfdata);
System.exit (1); // WARN
}
if( dbgroup.equals("sybase") && dbversion.equals(version) && required.equals("crit-vuln")) {
if(! remarks.equals("")) { perfdata = remarks; }
System.out.println("Version CRITICAL: " + version + " vulnerable (high risk)" + "|" + perfdata);
System.exit (2); // CRITICAL
}
counter++;
}
// the OS version is not listed, we don't know exactly if its good or bad.
System.out.println("Version UNKNOWN: "+version+ " unverified" + "|" + perfdata);
System.exit (3); // UNKNOWN;
}
}
}