001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.runtime.plugins.admin.jvmstatus.monitoring.alerts; 017 018import java.util.Map; 019 020import org.ametys.runtime.i18n.I18nizableText; 021import org.ametys.runtime.plugins.admin.jvmstatus.monitoring.SampleManager; 022 023/** 024 * Interface to be implemented for monitoring samples of data able to send system alerts when reaching threshold. 025 */ 026public interface AlertSampleManager extends SampleManager 027{ 028 /** 029 * Provides the threshold for each datasource. 030 * @return the threshold for each datasource. The key of the map is the datasource name for 031 * which you want to create an alert. 032 */ 033 public Map<String, Threshold> getThresholdValues(); 034 035 /** 036 * Represents a threshold 037 */ 038 public class Threshold 039 { 040 /** The possible types of operators */ 041 public static enum Operator 042 { 043 /** Alert will be triggered when the value is less or equal to the threshold */ 044 LEQ, 045 /** Alert will be triggered when the value is greater or equal to the threshold */ 046 GEQ, 047 } 048 049 private Operator _operator; 050 private String _dsName; 051 private Object _value; 052 private I18nizableText _mailSubject; 053 private I18nizableText _mailBody; 054 055 /** 056 * Creates a threshold. 057 * @param operator The kind of operator 058 * @param datasourceName The id of the datasource 059 * @param value The value of the threshold. Can be null to disable the alert. 060 * @param mailSubject The subject of the potential mail to send. 061 * @param mailBody The body of the potential mail to send. 062 */ 063 public Threshold(Operator operator, String datasourceName, Object value, I18nizableText mailSubject, I18nizableText mailBody) 064 { 065 _operator = operator; 066 _dsName = datasourceName; 067 _value = value; 068 _mailSubject = mailSubject; 069 _mailBody = mailBody; 070 } 071 072 /** 073 * Gets the datasource name of this threshold. 074 * @return the datasource name. 075 */ 076 public String getDatasourceName() 077 { 078 return _dsName; 079 } 080 081 /** 082 * Gets the value of the threshold. 083 * @return the value of the threshold. 084 */ 085 public Object getValue() 086 { 087 return _value; 088 } 089 090 /** 091 * Gets the subject of the mail to send. 092 * @return the subject of the mail to send. 093 */ 094 public I18nizableText getMailSubject() 095 { 096 return _mailSubject; 097 } 098 099 /** 100 * Gets the body of the mail to send. 101 * @return the body of the mail to send. 102 */ 103 public I18nizableText getMailBody() 104 { 105 return _mailBody; 106 } 107 108 /** 109 * Tests if the given value exceeds the threshold. 110 * @param comparedTo The value to test against the threshold. 111 * @return true if it exceeded, false otherwise. 112 */ 113 public boolean isExceeded(Object comparedTo) 114 { 115 if (_value == null) 116 { 117 return false; 118 } 119 120 // integer values 121 if (_value instanceof Long) 122 { 123 Long longComparedTo; 124 if (comparedTo instanceof Long) 125 { 126 longComparedTo = (Long) comparedTo; 127 } 128 else if (comparedTo instanceof Integer) 129 { 130 longComparedTo = Long.valueOf((Integer) comparedTo); 131 } 132 else 133 { 134 return false; // The provided object cannot be cast => log ? 135 } 136 137 if (_operator.equals(Operator.GEQ)) 138 { 139 return longComparedTo >= ((Long) _value); 140 } 141 else if (_operator.equals(Operator.LEQ)) 142 { 143 return longComparedTo <= ((Long) _value); 144 } 145 } 146 147 // floating values 148 if (_value instanceof Double) 149 { 150 Double doubleComparedTo; 151 if (comparedTo instanceof Double) 152 { 153 doubleComparedTo = (Double) comparedTo; 154 } 155 else if (comparedTo instanceof Float) 156 { 157 doubleComparedTo = Double.valueOf((Float) comparedTo); 158 } 159 else 160 { 161 return false; // The provided object cannot be cast => log ? 162 } 163 164 if (_operator.equals(Operator.GEQ)) 165 { 166 return doubleComparedTo >= ((Double) _value); 167 } 168 else if (_operator.equals(Operator.LEQ)) 169 { 170 return doubleComparedTo <= ((Double) _value); 171 } 172 } 173 174 return false; 175 } 176 } 177}