zabbix oracle monitor

This commit is contained in:
netkiller
2016-12-15 13:58:33 +08:00
parent 9dae2851ee
commit f21eaaf5d2
11 changed files with 360 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/Oracle/target
/Oracle/*.log

27
Oracle/.classpath Normal file
View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/ojdbc6.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
Oracle/.project Normal file
View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>zabbix</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding/<project>=UTF-8

View File

@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5

View File

@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

BIN
Oracle/lib/ojdbc6.jar Normal file

Binary file not shown.

BIN
Oracle/lib/ojdbc7.jar Normal file

Binary file not shown.

118
Oracle/pom.xml Normal file
View File

@@ -0,0 +1,118 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.netkiller</groupId>
<artifactId>zabbix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zabbix</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<!-- <version>12.1.0.1</version> -->
<version>11.2.0.3</version>
<!-- <scope>system</scope> <systemPath>${basedir}/lib/ojdbc6.jar</systemPath> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<!-- <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source /> <target /> </configuration> </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.netkiller.zabbix.Oracle</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>cn.netkiller.Oracle</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<inherited>false</inherited>
<executions>
<execution>
<id>install:com.oracle:ojdbc6:11g</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${project.basedir}/lib/ojdbc6.jar</file>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
<packaging>jar</packaging>
<createChecksum>true</createChecksum>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,139 @@
package cn.netkiller.zabbix;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.logging.*;
/**
* Hello world!
*
*/
class LogFormatter extends Formatter {
@Override
public String format(LogRecord record) {
return String.format("%s %s\t%s\r\n", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()),
record.getLevel(), record.getMessage());
}
}
public class Oracle {
String url = null; // 数据库链接地址
String username = null;// 用户名,系统默认的账户名
String password = null;// 你安装时选设置的密码
Logger log = Logger.getLogger(this.getClass().getName());
public Oracle() {
FileHandler fileHandler = null;
try {
fileHandler = new FileHandler("monitor.%g.log");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.OFF);
log.addHandler(consoleHandler);
fileHandler.setLevel(Level.INFO);
this.log.addHandler(fileHandler);
fileHandler.setFormatter(new LogFormatter());
this.log.setLevel(Level.INFO);
}
public void openConfig() {
String connectionfig = "jdbc.properties";
Properties properties = new Properties();
try {
properties.load(new FileInputStream(connectionfig));
this.url = properties.getProperty("jdbc.url");
this.username = properties.getProperty("jdbc.username");
this.password = properties.getProperty("jdbc.password");
} catch (FileNotFoundException e) {
this.log.info(
e.getMessage() + " Working Directory = " + System.getProperty("user.dir") + "/" + connectionfig);
System.exit(1);
} catch (IOException e) {
this.log.info(e.getMessage());
System.exit(1);
}
if (this.url == null || this.username == null || this.password == null) {
this.log.info("This Propertie file is invalid");
System.exit(1);
// throw new Exception("");
}
}
public void testConnection() {
Connection connection = null;// 创建一个数据库连接
ResultSet result = null;// 创建一个结果集对象
Statement statement = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
connection = DriverManager.getConnection(this.url, this.username, this.password);
String sql = "select current_date from dual";
statement = connection.createStatement();
result = statement.executeQuery(sql);
if (result.next()) {
this.log.info(String.format("%s %s", result.getDate(1), result.getTime(1)));
System.out.println(1);
} else {
System.out.println(0);
}
} catch (ClassNotFoundException e) {
log.info(e.getMessage());
System.exit(1);
} catch (SQLException e) {
log.info(e.getMessage());
System.exit(1);
} finally {
try {
if (result != null) {
result.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e) {
this.log.info(e.getMessage());
System.exit(1);
}
}
}
public static void main(String[] args) {
try {
Oracle oracle = new Oracle();
oracle.openConfig();
if(args[1] == "--conn"){
oracle.testConnection();
}
} catch (Exception e) {
System.out.println(0);
}
}
}

View File

@@ -0,0 +1,38 @@
package cn.netkiller.zabbix;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest
extends TestCase
{
/**
* Create the test case
*
* @param testName name of the test case
*/
public AppTest( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( AppTest.class );
}
/**
* Rigourous Test :-)
*/
public void testApp()
{
assertTrue( true );
}
}