Skip to content

Instantly share code, notes, and snippets.

@bitsprint
Created July 29, 2018 13:12
Show Gist options
  • Save bitsprint/1a9c46767ca427f08b3fe8ff18c3063f to your computer and use it in GitHub Desktop.
Save bitsprint/1a9c46767ca427f08b3fe8ff18c3063f to your computer and use it in GitHub Desktop.
Add an Elastic Watch
function Add-ElasticWatch-v2($name, $body) {
$uri = "http://localhost:9200/_watcher/watch/" + $name
Add-ElasticWatch($name, $uri, $body)
}
function Add-ElasticWatch-v5($name, $body) {
$uri = "http://localhost:9200/_xpack/watcher/watch/" + $name
Add-ElasticWatch($name, $uri, $body)
}
function Add-ElasticWatch($name, $uri, $body) {
Invoke-WebRequest -Uri $uri -Method Put -Headers @{ Authorization = "Basic [AuthorizationToken]" } -Body $body
}
@bitsprint
Copy link
Author

bitsprint commented Jul 29, 2018

Add-ElasticWatch-v2 -name [NAME] -body '{   	
	"trigger" : { "schedule" : { "interval" : "1m" } },   	
	"input" : {     		
		"search" : {       			
			"request" : {         				
				"indices" : [ "logging-*" ],         				
				"body" : { 
					"query" : { 
						"bool" : { 
							"must" : [
								{
									"query_string" : { 
										"default_field" : "providerName",
										"query" : "[QUERY STRING]"
									}
								},
								{
									"query_string" : {
										"default_field" : "eventId",
										"query" : "16"
									}
								}]
							}
						}
					}
				}		
			}     		
		}   	
	},   	
	"actions": { 	 		
		"monitoring_webhook" : {  		 			
			"webhook" : { 		  				
				"method" : "POST",  		  				
				"host" : "localhost", 				
				"port": 8448, 				
				"path":"/api",  		  				
				"body" : "{ \"alertModelLevel\": \"0\", \"subject\":\"[SUBJECT]\", \"body\": \"{{#toJson}}ctx{{/toJson}}\" }"  		 			
			} 	 		
		}   	
	} 
}'

@bitsprint
Copy link
Author

ElasticSearch Logging – Deployment & configuration

  1. Browse the elastic search directory, e.g.
    $ cd C:\Program Files\elasticsearch-2.1.1\bin
  2. Install two plugins, License and Shield
$ plugin install license
$ plugin install shield
  1. Browse the shield directory, e.g.
    $ cd C:\Program Files\elasticsearch-2.1.1\bin\shield
  2. Create a new user named es_admin with the role admin, e.g.
    $ esusers useradd es_admin -r admin -p secret
  3. Update kibana.yml with user credentials
elasticsearch.username: es_admin
elasticsearch.password: secret
  1. Update service fabric projects with password - PackageRoot > Config > Settings.xml
 <Section Name="ElasticSearchEventListener">
    <Parameter Name="serviceUri" Value="http://localhost:9200" />
    <Parameter Name="indexNamePrefix" Value="logging" />
    <Parameter Name="enabled" Value="true" />
    <Parameter Name="userName" Value="es_admin" />
    <Parameter Name="password" Value="secret" />  
  </Section>
  1. Browse the elastic search directory, e.g.
    $ cd C:\Program Files\elasticsearch-2.1.1\bin

  2. Install watcher
    $ plugin install watcher

  3. Restart elastic service

  4. Add notification configuration in elasticsearch.yml (this will most likely be replaced by webhook), e.g.

watcher.actions.email.service.account:
    outlook_account:
        profile: outlook
        smtp:
            auth: true
            starttls.enable: true
            host: smtp-mail.outlook.com
            port: 587
            user: <username>
            password: <password>
  1. Add a watch - named 'log_servicetyperegistered_watch', example notifies when ServiceTypeRegistered event occurs in logging (authorization header is base64 encoded username:password)
    Email example:
curl -XPUT 'http://localhost:9200/_watcher/watch/log_servicetyperegistered_watch' -H "Authorization: Basic [AuthorisationToken]" -d '{
  "trigger" : {
    "schedule" : { "interval" : "30m" } 
  },
  "input" : {
    "search" : {
      "request" : {
        "indices" : [ "logging-*" ],
        "body" : {
          "query" : {
            "match" : { "eventName": "ServiceTypeRegistered" }
          }
        }
      }
    }
  },
  "actions": {
    "send_email": { 
      "email": {
        "to": "user@org", 
        "subject": "Watcher Notification - ServiceTypeRegistered",
        "body": "A ServiceTypeRegistered message was received."
      }
    }
  }
}'

Webhook example **:

curl -XPUT 'http://localhost:9200/_watcher/watch/log_servicetyperegistered_watch' -H "Authorization: Basic [AuthorisationToken]" -d '{   
	"trigger" : {     
		"schedule" : { "interval" : "1m" }    
	},   
	"input" : {     
		"search" : {       
			"request" : {         
				"indices" : [ "logging-*" ],         
				"body" : {           
					"query" : {             
						"match" : { 
							"eventName": "ServiceTypeRegistered" 
						}           
					}         
				}       
			}     
		}   
	},   
	"actions": { 	
		"my_webhook" : {  		
			"webhook" : { 		  
				"method" : "POST",  		  
				"host" : "localhost", 
				"port": 8080, 
				"path":"/api",  		  
				"body" : "ServiceTypeRegistered events received: {{ctx.payload.hits.total}}"  		
			} 	
		}   
	} 
}'


  1. Monitor watches
    • Go to the Kibana Settings > Indices tab.
    • Enter .watch_history* in the Index name or pattern field.
    • Click in the Time field name field and select trigger_event.triggered_time
    • Go to the Discover tab to see the most recently executed watches.

** Note:
To test the webhook example above I used the following code to create a Nancy micro-service in LinqPad

void Main()
{
	using (var host = new Nancy.Hosting.Self.NancyHost(
						new Uri("http://localhost:8080"), 
						new LinqpadNancyBootstrapper(), 
						new HostConfiguration { UrlReservations = new UrlReservations { CreateAutomatically = true } }))
	{
		host.Start();
		Console.ReadLine();
	}
}

public class HelloModule : Nancy.NancyModule
{
	public HelloModule() : base("api")
	{
		Get["/"] = parameters => "Hello World";
		Post["/"] = parameters => { Context.Request.Body.AsString().Dump(); return HttpStatusCode.OK; };
	}
}

public class LinqpadNancyBootstrapper : Nancy.DefaultNancyBootstrapper
{
	protected override void ConfigureApplicationContainer(Nancy.TinyIoc.TinyIoCContainer container)
	{
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment