Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
chamerling committed Dec 14, 2011
0 parents commit a840a25
Show file tree
Hide file tree
Showing 31 changed files with 2,983 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
@@ -0,0 +1,19 @@
target/
modules/
lib/

#Eclipse
.project
.metadata
eclipse/
test-result/
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
.classpath
.settings/
.loadpath
6 changes: 6 additions & 0 deletions README.md
@@ -0,0 +1,6 @@
# HeartBeat Manager

Cheers!

Christophe H.
@chamerling
36 changes: 36 additions & 0 deletions app/Bootstrap.java
@@ -0,0 +1,36 @@
/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import models.Host;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
import play.test.Fixtures;

/**
* Bootstrap some init data to avoid some useless test for the application long
* life time
*
*
*/
@OnApplicationStart
public class Bootstrap extends Job {

@Override
public void doJob() throws Exception {
if (Host.count() == 0) {
Fixtures.load("initial-data.yml");
}
}
}
30 changes: 30 additions & 0 deletions app/controllers/AdminHost.java
@@ -0,0 +1,30 @@
/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controllers;

import models.Host;
import play.mvc.With;

/**
*
* @author chamerling
*
*/
@CRUD.For(Host.class)
@With(Secure.class)
public class AdminHost extends CRUD {

}
49 changes: 49 additions & 0 deletions app/controllers/Application.java
@@ -0,0 +1,49 @@
/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controllers;

import java.util.List;

import jobs.PingJob;
import models.Host;
import play.mvc.Controller;

/**
*
* @author chamerling
*
*/
public class Application extends Controller {

public static void index() {
List<Host> hosts = Host.findAll();
render(hosts);
}

public static void hostInfo(String id) {
index();
}

public static void getHosts() {
renderJSON(Host.findAll());
}

public static void checkStatus() {
PingJob job = new PingJob();
job.in(5);
index();
}
}
41 changes: 41 additions & 0 deletions app/controllers/Security.java
@@ -0,0 +1,41 @@
/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package controllers;

import play.Play;

/**
*
* @author chamerling
*
*/
public class Security extends Secure.Security {

static boolean authenticate(String username, String password) {
return username.equals(Play.configuration
.getProperty("secure.admin.username"))
&& password.equals(Play.configuration
.getProperty("secure.admin.password"));
}

static boolean check(String profile) {
if (profile.equals("Admin")) {
return session.get("username").equals(
Play.configuration.getProperty("secure.admin.username"));
}
return false;
}
}
56 changes: 56 additions & 0 deletions app/controllers/Services.java
@@ -0,0 +1,56 @@
/**
*
*/
package controllers;

/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.InputStreamReader;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import models.Host;
import play.mvc.Controller;

/**
* Exposed Web services
*
* @author chamerling
*
*/
public class Services extends Controller {

public static void status() {
System.out.println("Got a status call...");
Gson gson = new Gson();
Host newHost = gson.fromJson(new InputStreamReader(request.body), Host.class);

long current = System.currentTimeMillis();
// get the host or register a new one is needed
Host host = Host.getFromName(newHost.hostname);
if (host != null) {
host.lastPing = current;
host.lastSuccess = current;
host.save();
} else {
newHost.lastPing = current;
newHost.lastSuccess = current;
newHost.save();
}
}

}
70 changes: 70 additions & 0 deletions app/jobs/PingJob.java
@@ -0,0 +1,70 @@
/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jobs;

import java.util.ArrayList;
import java.util.List;

import notifiers.Mails;

import models.Host;
import play.jobs.Every;
import play.jobs.Job;
import play.libs.WS;
import play.libs.WS.HttpResponse;
import play.libs.WS.WSRequest;

/**
*
* @author chamerling
*
*/
@Every("1h")
public class PingJob extends Job<String> {

@Override
public void doJob() throws Exception {
System.out.println("Let's ping all...");

List<Host> hosts = Host.findAll();
List<Host> failures = new ArrayList<Host>();
for (Host host : hosts) {
long current = System.currentTimeMillis();
System.out.println("Calling host at " + host.hostname);
String call = host.hostname;
WSRequest request = WS.url(call).timeout("30s");
HttpResponse response = null;
host.lastPing = current;

try {
response = request.get();
int status = response.getStatus();
if (status == 200) {
host.lastSuccess = current;
} else {
failures.add(host);
}
} catch (Exception e) {
failures.add(host);
}
host.save();
}

if (failures.size() > 0) {
Mails.failures(failures);
}
}
}
56 changes: 56 additions & 0 deletions app/jobs/ReportingJob.java
@@ -0,0 +1,56 @@
package jobs;

/*
* Copyright 2011 Christophe Hamerling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.ArrayList;
import java.util.List;

import notifiers.Mails;

import org.apache.commons.mail.SimpleEmail;

import models.Host;
import play.Play;
import play.jobs.Every;
import play.jobs.Job;
import play.libs.Mail;

/**
* @author chamerling
*
*/
@Every("24h")
public class ReportingJob extends Job<String> {

/**
* Get all the reports and generate a summary to be sent to the manager
*/
@Override
public void doJob() throws Exception {
System.out.println("Fire Report");
List<Host> result = new ArrayList<Host>();
List<Host> hosts = Host.findAll();
for (Host host : hosts) {
if (host.lastPing != host.lastSuccess) {
result.add(host);
}
}

if (result.size() > 0) {
Mails.failures(result);
}
}
}

0 comments on commit a840a25

Please sign in to comment.