Created
July 29, 2018 13:12
-
-
Save bitsprint/1a9c46767ca427f08b3fe8ff18c3063f to your computer and use it in GitHub Desktop.
Add an Elastic Watch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
ElasticSearch Logging – Deployment & configuration
- Browse the elastic search directory, e.g.
$ cd C:\Program Files\elasticsearch-2.1.1\bin
- Install two plugins, License and Shield
$ plugin install license
$ plugin install shield
- Browse the shield directory, e.g.
$ cd C:\Program Files\elasticsearch-2.1.1\bin\shield
- Create a new user named es_admin with the role admin, e.g.
$ esusers useradd es_admin -r admin -p secret
- Update kibana.yml with user credentials
elasticsearch.username: es_admin
elasticsearch.password: secret
- 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>
-
Browse the elastic search directory, e.g.
$ cd C:\Program Files\elasticsearch-2.1.1\bin
-
Install watcher
$ plugin install watcher
-
Restart elastic service
-
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>
- 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}}"
}
}
}
}'
- 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
Uh oh!
There was an error while loading. Please reload this page.