Skip to content

Commit

Permalink
postgres probe
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrihinas committed Nov 17, 2014
1 parent 5fdd8ac commit c2554f4
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven Integration for Eclipse
#Mon Nov 17 15:25:55 EET 2014
#Mon Nov 17 15:34:13 EET 2014
version=0.0.1-SNAPSHOT
groupId=eu.celarcloud.cloud-ms
m2e.projectName=HAProxyProbe
Expand Down
4 changes: 4 additions & 0 deletions JCatascopia-CELAR-Probe-Library/PostgresProbe/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.classpath
.project
.settings/
target/*
16 changes: 16 additions & 0 deletions JCatascopia-CELAR-Probe-Library/PostgresProbe/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<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>
<parent>
<groupId>eu.celarcloud.cloud-ms</groupId>
<artifactId>JCatascopia-CELAR-Probe-Library</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>PostgresProbe</artifactId>
<dependencies>
<dependency>
<groupId>eu.celarcloud.cloud-ms</groupId>
<artifactId>JCatascopia-Probe-Package</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Level;

import eu.celarcloud.jcatascopia.probepack.Probe;
import eu.celarcloud.jcatascopia.probepack.ProbeMetric;
import eu.celarcloud.jcatascopia.probepack.ProbePropertyType;

/*
* NOTE:
* Postgres must be configured to accept remote connections in pg_hba.conf file
* and listening address in postgress.conf file must be set to *
*/
public class PostgresProbe extends Probe{

private static int DEFAULT_SAMPLING_PERIOD = 30;
private static String DEFAULT_PROBE_NAME = "PostgresProbe";

private static final String CONFIG_PATH = "postgres.properties";
private Properties config;

private Connection conn;
private PreparedStatement ps;

public PostgresProbe(String name, int freq) {
super(name, freq);

parseConfig();
String host = config.getProperty("postgres.host", "localhost");
String port = config.getProperty("postgres.port", "5432");
String username = config.getProperty("postgres.username","");
String password = config.getProperty("postgres.password", "");
String database = config.getProperty("postgres.database", "");

this.addProbeProperty(0, "pgActiveConnections", ProbePropertyType.INTEGER, "#", "total number of active connections to database");
this.addProbeProperty(1, "pgDBsizeKB", ProbePropertyType.INTEGER, "KB", "total disk usage of database in KB");
this.addProbeProperty(2, "pgBlocksDiskRead", ProbePropertyType.INTEGER, "#", "blocks read directly from disk or operating system since last checkpoint");
this.addProbeProperty(3, "pgBlocksCacheHit", ProbePropertyType.INTEGER, "#", "Blocks read from PostgreSQL Cache per second since last checkpoint");

this.conn = null;
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+database+"",username, password);
this.ps = conn.prepareStatement("SELECT numbackends, pg_database_size('"+database+"') as dbsize, blks_read, blks_hit " +
"FROM pg_stat_database WHERE datname='"+database+"'");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
this.writeToProbeLog(Level.SEVERE, e.getMessage());
}
catch (SQLException e) {
e.printStackTrace();
this.writeToProbeLog(Level.SEVERE, e.getMessage());
}
}

public PostgresProbe(){
this(DEFAULT_PROBE_NAME, DEFAULT_SAMPLING_PERIOD);
}

@Override
public ProbeMetric collect() {
HashMap<Integer,Object> values = null;
try {
ResultSet rs = this.ps.executeQuery();
if (rs.next()){
System.out.println("pgActiveConnections: "+rs.getString("numbackends")+", pgDBsizeKB: "+rs.getInt("dbsize")/1024+", pgBlocksDiskRead: "+rs.getString("blks_read")+", pgBlocksCacheHit: "+rs.getString("blks_hit"));
values = new HashMap<Integer,Object>();
values.put(0, Integer.parseInt(rs.getString("numbackends")));
values.put(1, rs.getInt("dbsize")/1024);
values.put(2, Integer.parseInt(rs.getString("blks_read")));
values.put(3, Integer.parseInt(rs.getString("blks_hit")));
}
}
catch (SQLException e) {
e.printStackTrace();
this.writeToProbeLog(Level.SEVERE, e.getMessage());
}
return new ProbeMetric(values);
}

@Override
public String getDescription() {
return "Probe that collects performance metrics from Postgres database";
}

//parse the configuration file
private void parseConfig(){
this.config = new Properties();
//load config properties file
try {
InputStream fis = getClass().getResourceAsStream(CONFIG_PATH);
config.load(fis);
if (fis != null)
fis.close();
}
catch (FileNotFoundException e){
this.writeToProbeLog(Level.SEVERE,"config file not found");
}
catch (IOException e){
this.writeToProbeLog(Level.SEVERE,"config file parsing error");
}
}

public static void main(String[] args) {
PostgresProbe p = new PostgresProbe();
p.activate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###Postgres Probe configuration properties###
postgres.host=109.231.121.144
postgres.port=5432
postgres.username=playgen
postgres.password=aDam3ntiUm
postgres.database=dataplay
1 change: 1 addition & 0 deletions JCatascopia-CELAR-Probe-Library/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<module>JCatascopia-Probe-Package</module>
<module>CassandraProbe</module>
<module>HAProxyProbe</module>
<module>PostgresProbe</module>
</modules>
</project>

0 comments on commit c2554f4

Please sign in to comment.