Skip to content

Commit

Permalink
simplify metadata config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Xiao committed Oct 13, 2016
1 parent bab9e0e commit 4a18688
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 46 deletions.
Expand Up @@ -2,7 +2,6 @@

import com.splunk.splunkjenkins.model.EventType;
import hudson.Extension;
import hudson.XmlFile;
import hudson.util.FormValidation;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
Expand All @@ -15,8 +14,11 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -68,16 +70,14 @@ public class SplunkJenkinsInstallation extends GlobalConfiguration {

public SplunkJenkinsInstallation(boolean useConfigFile) {
if (useConfigFile) {
XmlFile file = getConfigFile();
if (file.exists()) {
try {
String xmlText = file.asString();
file.getXStream().fromXML(xmlText, this);
} catch (IOException ex) {
LOG.log(Level.SEVERE, "failed to read " + getId() + ".xml", ex);
}
this.updateCache();
super.load();
//load default metadata
try (InputStream metaInput = this.getClass().getClassLoader().getResourceAsStream("metadata.properties")) {
metaDataProperties.load(metaInput);
} catch (IOException e) {
//ignore
}
this.updateCache();
}
}

Expand Down Expand Up @@ -125,8 +125,8 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
public FormValidation doCheckHost(@QueryParameter("value") String hostName) {
if (StringUtils.isBlank(hostName)) {
return FormValidation.warning(Messages.PleaseProvideHost());
}else if(hostName.endsWith("cloud.splunk.com") &&
!(hostName.startsWith("input-")|| hostName.startsWith("http-inputs-"))){
} else if (hostName.endsWith("cloud.splunk.com") &&
!(hostName.startsWith("input-") || hostName.startsWith("http-inputs-"))) {
return FormValidation.warning(Messages.CloudHostPrefix(hostName));
}
return FormValidation.ok();
Expand All @@ -151,7 +151,11 @@ public FormValidation doTestHttpInput(@QueryParameter String host, @QueryParamet
config.token = token;
config.useSSL = useSSL;
config.metaDataConfig = metaDataConfig;
config.enabled=true;
config.updateCache();
if (!config.isValid()) {
return FormValidation.error("Invalid config, please check Hostname or Token");
}
return verifyHttpInput(config);
}

Expand Down Expand Up @@ -186,7 +190,7 @@ protected void updateCache() {
metaDataProperties.load(new StringReader(metaDataConfig));
}
} catch (Exception e) {
LOG.log(Level.SEVERE, "invalid Splunk url ", e);
LOG.log(Level.SEVERE, "Invalid Splunk host " + host, e);
}
}

Expand Down Expand Up @@ -411,4 +415,12 @@ public String getMetadataHost() {
}
return getHostName();
}

public String getLocalHostName(){
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "yourhostname";
}
}
}
Expand Up @@ -109,7 +109,7 @@ private Map<String, String> getMetaData(SplunkJenkinsInstallation config) {
//just plain text, not complex object, prefer "httpevent" as sourcetype
metaDataBuilder
.putIfAbsent(EVENT_SOURCE_TYPE, config.getMetaData(eventType.getKey("sourcetype_text")))
.putIfAbsent(EVENT_SOURCE_TYPE, "httpevent");
.putIfAbsent(EVENT_SOURCE_TYPE, config.getMetaData("sourcetype_text"));
}
for (String metaDataKey : METADATA_KEYS) {
//individual config(EventType) have higher priority over default config
Expand Down
Expand Up @@ -23,7 +23,7 @@
<f:checkbox default="true"/>
</f:entry>
<f:entry title="${%MetaData}" field="metaDataConfig">
<f:textarea default="${%default_metadata}"/>
<f:textarea default="${%default_metadata(instance.getLocalHostName())}"/>
</f:entry>

<f:validateButton title="${%Test Connection}" progress="${%Testing...}" method="testHttpInput"
Expand Down
@@ -1,11 +1,7 @@
ConfigTitle=Splunk for Jenkins Configuration
Hostname=Splunk Host Name
default_metadata=\
source=jenkins\n\n\
#custom index, uncomment the settings after index created in Splunk\n\
#index=jenkins_statistics\n\
#build_report.index=jenkins\n#file.index=jenkins_artifact\n#console_log.index=jenkins_console\n\
#source types for plian text\n
#disable audit trail\n\
host={0}\
#audit trail is not enabled by default\n\
jenkins_config.enabled=false

#end of metadata
@@ -1,27 +1,17 @@
<div>
<p>Specifiy index, host, source, soucetype for the events, This can be used in Splunk query language such as
<b> index="main" source="jenkins" host="jenkins.domain" sourcetype="_json" </b>
<p>Specify index, host, source, soucetype for the events. </p>
</p> index is a data repository in Splunk, please create the 4 indexes in Splunk:
<span class="warning">
jenkins, jenkins_statistics, jenkins_console and jenkins_artifact
</span>
</p>
<p>
host is used to identify which Jenkins master is the source of the data.
the metadata can be used in Splunk query language such as <br/>
index="jenkins" host="servername" sourcetype="json:jenkins" <br/>
</p>

<p>
<a href="https://wiki.jenkins-ci.org/display/JENKINS/Splunk+Plugin+for+Jenkins">See more details</a>
</p>
<p>The index in Splunk to which the events will be indexed to. Defaults to "main" index in Splunk.
Note: If using a custom index, that index must be already exist on your Splunk instance. Splunk-Jenkins will not
create it for you.</p>
<p>The metadata can be customized for each event prefix: <b>test_result, console_log, queue_info, file,
xml_config</b>
e.g.
<pre>
#index
index=main #this is global level
console_log.index=ci_log #only for console_log
#source
source=sandbox
#sourcetype
sourcetype=_json
#host
host=sandbox
</pre>
will set index as ci_log for console_log event, main as index for others. jenkins_job as source for console_log
event,
sandbox as source for others
<br/>Note: double quote is not needed for values.
</pre>
</div>
12 changes: 12 additions & 0 deletions splunk-devops/src/main/resources/metadata.properties
@@ -0,0 +1,12 @@
source=jenkins
#default index and source type settings
build_report.index=jenkins
build_event.index=jenkins_statistics
queue_info.index=jenkins_statistics
jenkins_config.index=jenkins_statistics
slave_info.index=jenkins_statistics
console_log.index=jenkins_console
log.index=jenkins_console
file.index=jenkins_artifact
sourcetype=json:jenkins
sourcetype_text=text:jenkins

0 comments on commit 4a18688

Please sign in to comment.