Heartbeat Check | Xitoring Document

Heartbeat Check

a Heartbeat is a signal sent by the service or application at regular intervals (e.g. every few seconds) to Xitoring. our system then checks whether it has received the heartbeat within a certain time frame. If it hasn't, it assumes that the service or application is not responding correctly and triggers an incident.

How a Heartbeat Check Works

When you create a Heartbeat check you just need to select a label like every other check and configure proper trigger and notification roles. the most important part is the Fault tolerance, for example, if you set the fault tolerance to 1 (minute) Xitoring will expect to receive a heartbeat at least every 1 minute. if the interval between a heartbeat to another exceeds more than the fault tolerance you will get an incident report.

after you create the heartbeat check you get a link like below:

https://node.xitoring.com/heartbeat/1000rquike6143511677680410
1

making GET requests to that link will define as a heartbeat. so you can add something like the below to any script or code to make sure that it is running smoothly.

curl https://node.xitoring.com/heartbeat/1000rquike6143511677680410
1

In the above example, we used curl to make a GET request to the heartbeat link, curl is a bash tool that you can use to contact HTTP services so if you put the following command in your crontab in a Linux server and make it run every 1 minute:

* * * * * curl https://node.xitoring.com/heartbeat/1000rquike6143511677680410
1

It can act as an overall availability check for your Linux machine, This is the simplest way to use a heartbeat check.

More Advanced use cases

Imagine you have a backup script that runs every hour and you want to make sure that it's being done regularly.

You need to create a Heartbeat check in the Xitoring panel and set the fault tolerance to 60 so that Xitoring expects to get a heartbeat every hour. (you can put something like "Production Server backup" in the label) upon creating the heartbeat check you get your Heartbeat check Link. You just need to add one line at the end of your backup script in the success part of your code like the below:

Bash script example

#!/bin/bash

# Set the backup directory
BACKUP_DIR=/path/to/backup/directory

# Set the directories to be backed up
DIR1=/path/to/first/directory
DIR2=/path/to/second/directory

# Set the backup filename
BACKUP_FILENAME="backup-$(date +%Y-%m-%d-%H-%M-%S).tar.gz"

# Create the backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
  mkdir -p "$BACKUP_DIR"
fi

# Create the backup archive
tar -czf "$BACKUP_DIR/$BACKUP_FILENAME" "$DIR1" "$DIR2"

# Print a message indicating the backup is complete
echo "Backup complete: $BACKUP_DIR/$BACKUP_FILENAME"

# Send a heartbeat to xitoring every time the backup script runs
curl https://node.xitoring.com/heartbeat/1000rquike6143511677680410
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Python example

import os
import shutil
import datetime
import requests

# Set the backup directory
BACKUP_DIR = "/path/to/backup/directory"

# Set the directories to be backed up
DIR1 = "/path/to/first/directory"
DIR2 = "/path/to/second/directory"

# Set the backup filename
backup_filename = "backup-{}.tar.gz".format(datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S"))

# Create the backup directory if it doesn't exist
if not os.path.isdir(BACKUP_DIR):
    os.makedirs(BACKUP_DIR)

# Create the backup archive
shutil.make_archive(os.path.join(BACKUP_DIR, backup_filename.replace(".tar.gz", "")), "gztar", DIR1, DIR2)

# Print a message indicating the backup is complete
print("Backup complete: {}".format(os.path.join(BACKUP_DIR, backup_filename)))

# Send a heartbeat to xitoring every time the backup script runs
response = requests.get("https://node.xitoring.com/heartbeat/1000rquike6143511677680410")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Last Updated: 8/31/2023, 6:06:24 PM