* INITIAL POPULATIONS OF THIS REPO

This commit is contained in:
2021-12-23 13:04:01 +08:00
parent 3165defa2e
commit ed70cad6ea
182 changed files with 22964 additions and 0 deletions
+33
View File
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-alarm</artifactId>
<version>2.0</version>
<licenses>
<license>
<name>The software is only allowed to be used in Four Elements - no license is given to any other parties</name>
</license>
</licenses>
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
+29
View File
@@ -0,0 +1,29 @@
<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>com.fourelementscapital</groupId>
<artifactId>lib</artifactId>
<version>${revision}</version>
</parent>
<groupId>com.fourelementscapital</groupId>
<artifactId>lib-alarm</artifactId>
<packaging>jar</packaging>
<name>lib-alarm</name>
<description>Alarm functions, call iMonitor API</description>
<url>http://www.fourelementscapital.com</url>
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,300 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.alarm;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Send alarm to iMonitor API
*/
public class Alarm {
private static final Logger log = LogManager.getLogger(Alarm.class.getName());
/**
* Private constructor
*/
private Alarm(){}
/**
* Construct alarm message with default parameters and send it to iMonitor API
* @param themes ThemeVO array list
* @param alarm_type Alarm type
* @param subject Subject
* @param message Message
* @param say Is Say
* @param email Is Email
* @param phone Is Phone
* @param attachmentfile Attachment file
* @param exc_add Exclude Address
* @throws IOException
*/
public static void sendAlarm(ArrayList<ThemeVO> themes, AlarmType alarm_type,String subject,String message, boolean say, boolean email, boolean phone, String attachmentfile,String exc_add) throws IOException {
if(themes!=null && themes.size()>0 && message!=null && alarm_type!=null){
// ignore alarm ? :
String c_noalarm=getConfigValue("ignore.alarm");
if(c_noalarm!=null && c_noalarm.equalsIgnoreCase("true")) {
//do nothing
}else{
//String xml=constructXMLWithFile(themes,subject,message,say,email,phone,attachmentfile,exc_add);
String socketMessage=constructSocketMessageWithFile(themes,subject,message,say,email,phone,attachmentfile,exc_add);
//log.debug("Sending alarm:xml:"+xml);
//log.debug("Sending alarm:socketMessage:"+socketMessage);
// check whether dump XML to file or send to iMonitor API :
boolean isDump = Boolean.parseBoolean(getConfigValue("alarm.dump"));
if (isDump) {
// create folder if not exist. format : 'yyyyMMdd' (today's date)
String dumpFolder = getConfigValue("alarm.dump.folder");
SimpleDateFormat sdfDumpFolder = new SimpleDateFormat("yyyyMMdd");
dumpFolder = dumpFolder + sdfDumpFolder.format(new Date()) + "/"; // setup today folder
File dir = new File(dumpFolder);
dir.mkdirs();
// write dump file. format : 'HHmmss.xml' (HHmmss : current time)
SimpleDateFormat sdfFile = new SimpleDateFormat("HHmmssSSS");
PrintWriter writer = new PrintWriter(dumpFolder + sdfFile.format(new Date()) + ".xml", "UTF-8");
//writer.println(xml);
//writer.println(socketMessage);
writer.close();
}
else {
//sendXML(xml); // send to iMonitor
sendSocketMessage(socketMessage); // send to iMonitor
}
}
}
}
/**
* Send phone alarm with list of theme and a message
* @param themes ThemeVO array list
* @param message Message
* @throws IOException
*/
public static void sendAlarm(ArrayList<ThemeVO> themes, String message) throws IOException {
Alarm.sendAlarm(themes,AlarmType.PHONE,null,message,false,false,true,null,null);
}
/**
* Send phone alarm with a theme and a message
* @param theme Theme
* @param message Message
* @throws IOException
*/
public static void sendAlarm(String theme, String message) throws IOException {
ArrayList<ThemeVO> themes = new ArrayList<ThemeVO>();
themes.add(new ThemeVO(theme));
Alarm.sendAlarm(themes,AlarmType.PHONE,null,message,false,false,true,null,null);
}
/**
* Construct XML with file attachment
* @param themes Themes
* @param subject Subject
* @param bodymsg Body message
* @param say Is say
* @param email Is email alert
* @param phone Is phone alert
* @param attachment Attachment
* @param exc_add Exclude Address
* @return XML
*/
private static String constructXMLWithFile(List<ThemeVO> themes,String subject,String bodymsg,boolean say,boolean email,boolean phone, String attachment,String exc_add) {
subject = escapeChar(subject);
String xml="<IMONITOR>";
for (int i=0; i<themes.size(); i++) {
ThemeVO themeVO = themes.get(i);
if (themeVO != null) {
xml+="<THEME>"+themeVO.getName()+"</THEME>";
}
}
xml+="<SUBJECT>"+subject+"</SUBJECT>";
xml+="<BODY><![CDATA["+bodymsg+"]]></BODY>";
xml+="<OVERRIDEALARMLEVEL>m</OVERRIDEALARMLEVEL> ";
xml+="<SAY>"+(say+"").toUpperCase()+"</SAY> ";
xml+="<EMAIL>"+(email+"").toUpperCase()+"</EMAIL>";
xml+="<PHONE>"+(phone+"").toUpperCase()+"</PHONE>";
xml+="<ESCALATETICKS>5</ESCALATETICKS>";
xml+="<ESCALATEINTERVAL>15</ESCALATEINTERVAL>";
if(attachment!=null){
xml+="<EMAILIMAGES>"+attachment+"</EMAILIMAGES>";
}
if(exc_add!=null){
xml+="<EXCLUDEADDRESS>"+exc_add+"</EXCLUDEADDRESS>";
}
xml+="</IMONITOR>";
return xml;
}
/**
* Construct message with file attachment
* @param themes Themes
* @param subject Subject
* @param bodymsg Body message
* @param say Is say
* @param email Is email
* @param phone Is phone
* @param attachment Attachment
* @param exc_add Exclude Address
* @return XML
*/
private static String constructSocketMessageWithFile(List<ThemeVO> themes,String subject,String bodymsg,boolean say,boolean email,boolean phone, String attachment,String exc_add) {
subject = escapeChar(subject);
String message = "";
for (int i=0; i<themes.size(); i++) {
ThemeVO themeVO = themes.get(i);
if (themeVO != null) {
message+="~theme$#="+themeVO.getName();
}
}
message+="~subject$#="+subject;
message+="~body$#="+bodymsg;
//message+="~recipients$#="+theme;
message+="~sayIt$#="+(say+"").toUpperCase();
message+="~emailIt$#="+(email+"").toUpperCase();
message+="~phoneCall$#="+(phone+"").toUpperCase();
message+="~emailAttachments$#="+attachment;
//message+="~ym$#="+theme;
//message+="~sms$#="+theme;
message+="<EOF>";
// remove first char : '~'
message=message.substring(1, message.length());
/*
xml+="<OVERRIDEALARMLEVEL>m</OVERRIDEALARMLEVEL> ";
xml+="<ESCALATETICKS>5</ESCALATETICKS>";
xml+="<ESCALATEINTERVAL>15</ESCALATEINTERVAL>";
if(attachment!=null){
xml+="<EMAILIMAGES>"+attachment+"</EMAILIMAGES>";
}
if(exc_add!=null){
xml+="<EXCLUDEADDRESS>"+exc_add+"</EXCLUDEADDRESS>";
}
*/
return message;
}
/**
* Send XML to iMonitor API
* @param xml XML
* @throws IOException
*/
private static void sendXML(String xml) throws IOException {
try{
String url=getConfigValue("imonitor.url");
PostMethod post = new PostMethod(url);
try {
post.setRequestEntity(new StringRequestEntity(xml,"text/xml",null ));
post.setRequestHeader("Content-type", "text/xml; utf-8");
HttpClient httpclient = new HttpClient();
int result = httpclient.executeMethod(post);
} catch (IOException e) {
// e.printStackTrace();
throw e;
} finally {
post.releaseConnection();
}
}catch(Exception e){
e.printStackTrace();
throw e;
}
}
/**
* Send message via Socket to iMonitor API
* @param message Message
* @throws Exception
*/
private static void sendSocketMessage(String socketMessage) throws IOException {
String ip = getConfigValue("imonitor.ip");
int port = Integer.parseInt(getConfigValue("imonitor.port"));
Socket socket = new Socket(ip, port);
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream(), "UTF-8");
osw.write(socketMessage, 0, socketMessage.length());
osw.flush();
socket.close();
}
/**
* Escape XML characters
* @param str String
* @return String with escape characters
*/
private static String escapeChar(String str) {
String result = null;
if (str != null) {
result = str.replaceAll("&", "&amp;");
result = result.replaceAll("\"", "&quot;");
result = result.replaceAll("'", "&apos;");
result = result.replaceAll("<", "&lt;");
result = result.replaceAll(">", "&gt;");
}
return result;
}
/**
* Get the value of a property defined in config file
* @param propertyName - the name or key of the property you want to retrieve
* @return the string value
* @throws IOException
*/
private static String getConfigValue(String propertyName) throws IOException
{
Properties prop = new Properties();
String propFileName = "config_alarm.properties";
InputStream inputStream = Alarm.class.getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
// get the property value and print it out
String value = prop.getProperty(propertyName);
return value;
}
}
@@ -0,0 +1,17 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.alarm;
/**
* Enumeration class for type of the Alarm
*/
public enum AlarmType {
EMAIL, PHONE
}
@@ -0,0 +1,40 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.alarm;
import java.io.Serializable;
/**
* Theme Value Object to store theme name used by Alarm
*/
public class ThemeVO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Theme name
*/
private String name;
/**
* Constructor with name parameter
*/
public ThemeVO(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
+7
View File
@@ -0,0 +1,7 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Windows peer couldn't start Rserve</li>
<li>Make sure there is only one tomcat and Rserve process in the peer</li>
<li>Restart tomcat and Rserve</li>
</ul>
</div>
+8
View File
@@ -0,0 +1,8 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Peer connection pool for managing multiple RServe connections are not working properly</li>
<li>On unix peer, this may happen when Rserve is not started</li>
<li>On windows, Rserve port used by someother program </li>
</ul>
</div>
+5
View File
@@ -0,0 +1,5 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>This is uncategorized error due to certain Runtime error messages on peer or Rengine/Rserve</li>
</ul>
</div>
+7
View File
@@ -0,0 +1,7 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Null Error message by RServe/REngine at the end of execution of script. According to RForge documentation, it is classified as unknow and something went wrong, Check http://www.rforge.net/Rserve/faq.html for more info</li>
<li>Your script might not be reached end and could have been Runtime error in the middle</li>
<li>Try running your script with low memory usage settings on your R</li>
</ul>
</div>
+6
View File
@@ -0,0 +1,6 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>R script thrown Runtime exception</li>
<li>Make sure runtime exceptions from Database,Network and File resources are caught and written in console</li>
</ul>
</div>
+8
View File
@@ -0,0 +1,8 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>If the script is taking more than specified time, then it will be removed from the queue not matter eventually successful or failure</li>
<li>Consider increasing execution time out by clicking speaker icon above the live queue of the scheduler</li>
<li>Code modified and the new code consumes more time than previous executions</li>
<li>This will also happen when peer restarted or stopped while task is running</li>
</ul>
</div>
+7
View File
@@ -0,0 +1,7 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>The task still in the queue because peer couldn't reach server to update the status of task and server removes it as later found no longer running in peer</li>
<li>Peer was running memory intensive script or unix peer Rserve threads were fully utilized at that time</li>
<li>Please check the queue history later to know the final status of the execution</li>
</ul>
</div>
+5
View File
@@ -0,0 +1,5 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>No Room to Execute the Script because peer receives 2 tasks at the same time and accepts only one and the other sent back to server and it will be resent to different peer</li>
</ul>
</div>
+6
View File
@@ -0,0 +1,6 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Task removed because peer says it is no longer running in peer</li>
<li>This can happen if peer restarted</li>
</ul>
</div>
+6
View File
@@ -0,0 +1,6 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Any run time exception message at peer side, usually this will be thrown when if there was problem in database connectivity, unexpected value, unchecked null vale and so on</li>
<li>Make sure runtime exceptions from Database,Network and File resources are caught and written in console</li>
</ul>
</div>
+6
View File
@@ -0,0 +1,6 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>Any runtime exception message on server side,usually this will be thrown when there is problem in database connectivity, unexpected value, unchecked null vale and so on. </li>
<li>Make sure runtime exceptions from Database,Network and File resources are caught and written in console</li>
</ul>
</div>
+5
View File
@@ -0,0 +1,5 @@
<div style='margin:10px 0px 10px 0px border:1px solid #b0b0b0;padding:5px'>
<ul style="list-style-type:circle;padding-left: 5px;margin-top: 0px;">
<li>This will happen when the next trigger time of the script arrived and still current triggered execution is waiting because of one or more dependecies not successfull.</li>
</ul>
</div>
+12
View File
@@ -0,0 +1,12 @@
ignore.alarm=false
#imonitor.url=http://10.153.64.31:5001/iMonitor/Responder.aspx
imonitor.ip=127.0.0.1
imonitor.port=1777
#dump XML to file instead of sending it to iMonitor API
alarm.dump=false
#alarm.dump=true
#alarm.dump.folder=/temp/scheduler-alarm/
+13
View File
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%4p %d{HH:mm:ss,SSS} %C - %m%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="CONSOLE"/>
</Root>
</Loggers>
</Configuration>
@@ -0,0 +1,54 @@
/******************************************************************************
*
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
* All rights reserved.
*
******************************************************************************/
package com.fourelementscapital.alarm;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import junit.framework.TestCase;
/**
* lib-alarm unit test
*/
public class LibAlarmTest extends TestCase {
/**
* Test send alarm
*/
public void testSendAlarm()
{
try{
ArrayList<ThemeVO> themes=new ArrayList<ThemeVO>();
themes.add(new ThemeVO("computing"));
themes.add(new ThemeVO("etrading"));
// Remove the comment tag to test. The code are commented out to prevent sending alarm when installing this lib :
// send alarm (default) :
//Alarm.sendAlarm( themes, AlarmType.EMAIL, "Test subject", "Test message", false, true, false, null, null);
// send alarm with list of theme & a message :
//Alarm.sendAlarm( themes, "Test message 2");
// send alarm with a theme & a message :
//Alarm.sendAlarm( "computing", "Test message 3");
//JOptionPane.showMessageDialog(null, "send socket message done");
assertTrue(true);
}catch(Exception e){
e.printStackTrace();
assertTrue(false);
}
}
}