At minimum, you only need to specify the url
you want to monitor. For example to monitor github.com
:
probes:- requests:- url: https://github.com
If you didn't define the http method, it will use the GET method by default. Please note that with this configuration, you will not get any notifications when the site github.com is down since the notification configuration is not defined.
Probes are more useful with notifications enabled to alert when something is amiss. The following is an example configuration to get notification via e-mail when an incident occurs:
notifications:- id: unique-id-smtptype: smtpdata:recipients:- YOUR_EMAIL_ADDRESS_HEREhostname: smtp.gmail.comport: 587username: YOUR_GMAIL_ACCOUNTpassword: YOUR_GMAIL_PASSWORD_OR_APP_PASSWORDprobes:- id: '1'name: Monika Landing Pagedescription: Landing page of awesome Monikainterval: 10 # in secondsrequests:- url: https://hyperjumptech.github.io/monikatimeout: 7000 # in millisecondsalerts:- assertion: response.status < 200 or response.status > 299message: Target is not healthy. It has not been returning status code 2xx.- assertion: response.time > 2000 # in millisecondsmessage:Target is not healthy. The response time has been greater than 2000ms.
Using the above configuration, Monika will check the landing page every 10 seconds and will send a notification by email when the landing page is down 5 times in a row. For more information about available notification channels, refer to Notifications.
You can also send POST requests using Monika. The following is an example of sending a POST request to simulate HTML form submission.
probes:- id: '1'name: HTML form submissiondescription: simulate html form submissioninterval: 10 # in secondsrequests:- method: POSTurl: http://www.example.com/login.phptimeout: 7000 # in millisecondsheaders:Content-Type: application/x-www-form-urlencodedbody:username: someusernamepassword: somepassword
Using the configuration above, Monika will send a POST request to http://www.example.com/login.php
with the defined request's body.
Monika supports sending multiple requests one after another in a single probe. Below is one such configuration:
probes:- id: '1'name: Simulate form submitdescription: simulate html form submissioninterval: 15 # in secondsrequests:- method: GETurl: https://github.com/timeout: 7000 # in millisecondssaveBody: false- method: GETurl: https://github.com/hyperjumptechtimeout: 7000 # in millisecondssaveBody: truealerts:- assertion: response.status > 299message: Target is not healthy. It has not been returning status code 2xx.- assertion: response.time > 2000 # in millisecondsmessage:Target is not healthy. The response time has been greater than 2000ms.
In the configuration above, Monika will first check https://github.com/
then https://github.com/hyperjumptech
. If the status code of https://github.com/
is not 2xx (e.g., 200, 201), Monika will not check https://github.com/hyperjumptech
.
If there is a case where executing a GET request to https://github.com
triggers an alert, the next request will not be executed.
Monika supports request chaining, which enables you to do multiple requests and the ability to use past responses from earlier requests. For example, after executing a GET request to a certain API, the next request could use the previous request(s) response into their path/assertion parameters or headers.
Here is an example on how you could get previous request(s) response data into your next request:
{{ responses.[0].status }} ==> Get status code from first request response{{ responses.[1].body.token }} ==> Get token from second request response{{ responses.[2].headers.SetCookie[0] }} ==> Get first cookie from third request response
In the example above, responses.[0]
refers to the response from the first request in the probe, responses.[1]
refers to the response from the second request in the probe, and so on. Please note that you can only use the response from previous requests in the same probe.
Please refer to Probe Response Anatomy in order to know which value could be used from the response body for the next request(s).
In the sections below, you can find several examples of configuration files which contain chaining requests.
Here is an example of using previous request's response in the path/assertion parameters:
probes:- id: '1'name: Simulate form submitdescription: simulate html form submissioninterval: 15 # in secondsrequests:- method: GETurl: https://reqres.in/api/userstimeout: 7000 # in milliseconds- method: GETurl: https://reqres.in/api/users/{{ responses.[0].body.data.[0].id }}timeout: 7000 # in millisecondsalerts:- assertion: response.status > 299message: Target is not healthy. It has not been returning status code 2xx.- assertion: response.time > 2000 # in millisecondsmessage:Target is not healthy. The response time has been greater than 2000ms.
In the configuration above, the first request will fetch all users from https://reqres.in/api/users
. Then in the second request, Monika will fetch the details of the first user from the first request. If there are no triggered alerts, the response returned from the first request is ready to be used by the second request using values from {{ responses.[0].body }}
.
Let's say the response from fetching all users in JSON format is as follows:
{"page": 2,"per_page": 6,"total": 12,"total_pages": 2,"data": [{"id": 7,"email": "michael.lawson@reqres.in","first_name": "Michael","last_name": "Lawson","avatar": "https://reqres.in/img/faces/7-image.jpg"},...]}
To use the user ID of the first user in the second request, we define the url of the second request as {{ responses.[0].body.data.[0].id }}
.
Here is an example of using previous request's response in the headers:
probes:- id: '1'name: Simulate form submitdescription: simulate html form submissioninterval: 15 # in secondsrequests:- method: POSTurl: https://reqres.in/api/logintimeout: 7000 # in millisecondsbody:email: eve.holt@reqres.inpassword: cityslicka- method: POSTurl: https://reqres.in/api/users/timeout: 7000 # in millisecondsbody:name: morpheusjob: leaderheaders:Authorization: Bearer {{ responses.[0].body.token }}alerts:- assertion: response.status > 299message: Target is not healthy. It has not been returning status code 2xx.- assertion: response.time > 2000 # in millisecondsmessage:Target is not healthy. The response time has been greater than 2000ms.
Using the above configuration, Monika will perform a login request in the first request, then use the returned token in the Authorization header of the second request.
Continuing with the examples from www.reqres.in above, say we would like to use the previous GET request to perform a POST /login. If the data from the initial request is something like below:
{"data":{"id": 1,"email": "george.bluth@reqres.in","first_name": "George","last_name": "Bluth","avatar": "https://reqres.in/img/faces/1-image.jpg"},....}
Then you can use the user's email in the login request body as follows:
probes:- id: probe-01name: 'body from response'interval: 15 # in secondsrequests:- url: https://reqres.in/api/users/1method: GETtimeout: 5000 # in millisecondssaveBody: falseheaders:Content-Type: application/json; charset=utf-8- url: https://reqres.in/api/loginmethod: POSTtimeout: 1000 # in millisecondsheaders:Content-Type: application/json; charset=utf-8body:email: '{{ responses.[0].body.data.email }}'password: passwordalerts:- assertion: response.status != 200message: Http Response status code is not 200!notifications:- id: unique-id-desktoptype: desktop
Note: Please do not forget the single quotes before and after the opening and closing double braces to explicitly indicate a string value. YAML parsers will generate warnings without it.
In a request chaining mode, if one request fails, Monika does not continue with the next request in the probe. The reasoning is that, if an earlier request, say a GET /token fails, it would be pointless to continue and fetch /userdata.
If you want to continue the next request even when the first one fails, we recommend putting your requests in multiple probes, something like this:
probes:- id: get-username: 'get-user'description: login and check user app datainterval: 30requests:- url: https://example.com/tokenbody: {user: 'xxxx', password: 'yyyy'}method: POST- url: https://example.com/userdatamethod: GETheaders:Authorization: Bearer {{ responses.[0].body.accessToken }}- id: get-mailname: 'get-mail'description: fetching user emailsinterval: 30requests:- url: https://example.com/tokenbody: {user: 'xxxx', password: 'yyyy'}method: POSTtimeout: 10000method: POST- url: https://example.com/usermailmethod: GETheaders:Authorization: Bearer {{ responses.[0].body.accessToken }}
In the example above, the get-email will run even if get-user has failed.
PT Artha Rajamas Mandiri (Hyperjump) is an open-source-first company providing engineering excellence service. We aim to build and commercialize open-source tools to help companies streamline, simplify, and secure the most important aspects of its modern DevOps practices.
Copyright © 2024 Hyperjump Tech. All Rights Reserved.