Skip to content

Commit

Permalink
verify provider configs
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz99 committed Dec 29, 2022
1 parent f179e15 commit 10d1dd4
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 2.4.5-SNAPSHOT

## 2.4.4-SNAPSHOT

* [CHANGE] Removed statistics view (content moved elsewhere)
Expand Down
12 changes: 0 additions & 12 deletions providers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ providers:
uri: /alertmonitor/webhook/promvm
params:
server: https://promvm/prometheus
clientPoolSize: 1
clientConnectTimeout: 10
clientReadTimeout: 120
syncInterval: 120
- name: swarm1
type: prometheus
Expand Down Expand Up @@ -37,16 +34,7 @@ providers:
uri: /alertmonitor/webhook/eventlogger
params:
server: http://swarm1:7073/eventlogger
clientPoolSize: 1
clientConnectTimeout: 10
clientReadTimeout: 120
syncInterval: 0
- name: dummy
type: prometheus
uri: /alertmonitor/webhook/dummy
params:
server: http://dummy:12345/dummy
clientPoolSize: 10
clientConnectTimeout: 10
clientReadTimeout: 120
syncInterval: 0
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import si.matjazcerkvenik.alertmonitor.util.MD5;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigReader {

Expand All @@ -37,7 +39,7 @@ public static YamlConfig loadProvidersYaml(String path) {
InputStream inputStream = new FileInputStream(f);
YamlConfig config = yaml.load(inputStream);
LogFactory.getLogger().info("providers config loaded: " + f.getAbsolutePath());
verifyConfigAndSetDefaults(config.getProviders());
verifyConfigs(config.getProviders());
return config;
} catch (FileNotFoundException e) {
LogFactory.getLogger().warn("ConfigReader: no file providers.yml found at " + path);
Expand All @@ -53,23 +55,30 @@ public static YamlConfig loadProvidersYaml(String path) {
* Check all parameters if they suit the selected provider type and set default values where needed.
* @return true if config is valid
*/
public static List<ProviderConfig> verifyConfigAndSetDefaults(List<ProviderConfig> configs) throws ConfigException {
public static List<ProviderConfig> verifyConfigs(List<ProviderConfig> configs) throws ConfigException {

if (configs == null) throw new ConfigException("config is null");

for (ProviderConfig pc : configs) {

if (pc.getName() == null) pc.setName("Provider_" + pc.hashCode());
// check mandatory parameters
LogFactory.getLogger().info("checking provider config: " + pc.getName());
if (pc.getName() == null) throw new ConfigException("missing provider name");
pc.setId(MD5.getChecksum(pc.getName()));
if (pc.getUri() == null) throw new ConfigException("missing uri parameter");
if (pc.getUri() == null) throw new ConfigException("missing provider uri");
if (pc.getType() == null) throw new ConfigException("missing provider type");

// add empty map if whole params section is missing, it will be filled below
if (pc.getParams() == null) pc.setParams(new HashMap<>());

if (pc.getType().equalsIgnoreCase("prometheus")) {
// TODO check params
Object connTim = pc.getParams().get(PrometheusDataProvider.DP_PARAM_KEY_CLIENT_CONNECT_TIMEOUT_SEC);
LogFactory.getLogger().info("connT: " + connTim.toString());
if (connTim == null || connTim.toString().length() == 0) {
connTim = "10";
pc.setParam(PrometheusDataProvider.DP_PARAM_KEY_CLIENT_CONNECT_TIMEOUT_SEC, String.valueOf(connTim));
LogFactory.getLogger().info("default 10 set");
} else {
// check if number
}

checkParam(pc.getParams(), PrometheusDataProvider.DP_PARAM_KEY_SERVER, "http://undefined-hostname-config:9090");
checkParam(pc.getParams(), PrometheusDataProvider.DP_PARAM_KEY_CLIENT_POOL_SIZE, "1");
checkParam(pc.getParams(), PrometheusDataProvider.DP_PARAM_KEY_CLIENT_CONNECT_TIMEOUT_SEC, "10");
checkParam(pc.getParams(), PrometheusDataProvider.DP_PARAM_KEY_CLIENT_READ_TIMEOUT_SEC, "60");
checkParam(pc.getParams(), PrometheusDataProvider.DP_PARAM_KEY_SYNC_INTERVAL_SEC, "60");

} else if (pc.getType().equalsIgnoreCase("eventlogger")) {

} else {
Expand All @@ -82,4 +91,17 @@ public static List<ProviderConfig> verifyConfigAndSetDefaults(List<ProviderConfi
return configs;
}

/**
* Check if param exists. If not, then fill it with default value.
* @param params
* @param defaultValue
*/
private static void checkParam(Map<String, Object> params, String paramName, String defaultValue) {
Object p = params.get(paramName);
if (p == null) {
params.put(paramName, defaultValue);
LogFactory.getLogger().warn("param " + paramName + " is missing; default will be used: " + defaultValue);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2021 Matjaž Cerkvenik
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 si.matjazcerkvenik.alertmonitor.model.eventlogger;

public class EEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class UiReportBean implements Serializable {

private static final long serialVersionUID = 484765215984854L;

// @ManagedProperty(value="#{uiConfigBean}")
// private UiConfigBean uiConfigBean;

private AbstractDataProvider adp;

private final String QUERY_PROM_UP_TIME = "round(time()-process_start_time_seconds{job=\"prometheus\"})";
Expand All @@ -73,14 +70,6 @@ public AbstractDataProvider getAdp() {
return adp;
}

// public UiConfigBean getUiConfigBean() {
// return uiConfigBean;
// }
//
// public void setUiConfigBean(UiConfigBean uiConfigBean) {
// this.uiConfigBean = uiConfigBean;
// }

public String getPrometheusUpTime() {
PQueryMessage queryMessage = executeQuery(QUERY_PROM_UP_TIME);
if (queryMessage != null) {
Expand Down
5 changes: 0 additions & 5 deletions src/main/webapp/config/config.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
<f:selectItem itemLabel="TRACE" itemValue="TRACE"/>
</p:selectOneMenu>

<p:outputLabel value="Select Data Provider: " class="boldtext"/>
<p:selectOneMenu id="selectedDataProvider" value="#{uiConfigBean.selectedDataProvider}" dynamic="true" onchange="submit()">
<f:selectItems value="#{uiConfigBean.allDataProviderNames}"/>
</p:selectOneMenu>

<p:outputLabel value="Data retention (days): " class="boldtext"/>
<p:inplace editor="true">
<p:inputText value="#{uiConfigBean.dataRetention}" required="true" label="text"/>
Expand Down
1 change: 1 addition & 0 deletions src/main/webapp/providers/providers.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<f:facet name="title">
<p:outputLabel value="#{p.providerConfig.name}" class="boldtext" style="font-size: 20px;"/>
<am:warnings value="#{p.warnings}" />
<p:outputLabel value="Alarms: #{p.allActiveAlarmsCount}" style="margin-left: 20px; font-size: 12px;" />
</f:facet>

<p:panelGrid columns="2">
Expand Down
3 changes: 1 addition & 2 deletions src/main/webapp/templates/ptoolbar.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@

<p:menubar id="menubar">
<p:menuitem value="Providers" url="/alertmonitor/providers" icon="pi pi-fw pi-sitemap" />
<p:menuitem value="Active" url="/alertmonitor" icon="pi pi-fw pi-star" />
<p:menuitem value="Alarms" url="/alertmonitor" icon="pi pi-fw pi-star" />
<p:menuitem value="Query" url="/alertmonitor/query" icon="pi pi-fw pi-send" />
<p:submenu label="Targets" icon="pi pi-fw pi-th-large">
<p:menuitem value="by Instance" url="/alertmonitor/targets" icon="pi pi-fw pi-box"/>
<p:menuitem value="by Job" url="/alertmonitor/jobs" icon="pi pi-fw pi-bolt"/>
</p:submenu>
<p:submenu label="View" icon="pi pi-fw pi-list">
<p:menuitem value="Providers" url="/alertmonitor/providers" icon="pi pi-fw pi-sitemap" />
<p:menuitem value="Configuration" url="/alertmonitor/config" icon="pi pi-fw pi-cog" />
<p:menuitem value="About" url="/alertmonitor/about" icon="pi pi-fw pi-exclamation-circle" />
</p:submenu>
Expand Down

0 comments on commit 10d1dd4

Please sign in to comment.