Skip to content

Commit 760d285

Browse files
committed
Replace ping with heartbeat in the code and .ini
1 parent fc222bf commit 760d285

20 files changed

+200
-193
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This file is used to ignore files which are generated
22
# ----------------------------------------------------------------------------
33

4+
build/
45
.svn/
56
*~
67
*.autosave

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to this project will be documented in this file.
44

5-
## [Unreleased]
5+
## [1.1.0] - 2024-08-28
6+
7+
Replace ping with heartbeat in the code and .ini
8+
9+
### Changed
10+
11+
- Code, config.ini and test_child.py have been refactored
12+
13+
## [1.0.0] - 2024-06-07
614

715
- Initial release

README.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ This application is particularly useful in environments where multiple processes
2727

2828
## Configuration File : `config.ini`
2929
An example configuration file looks like this:
30-
Note that ping means heartbeat.
3130

3231
```ini
3332
[processWatchdog]
@@ -36,34 +35,34 @@ nWdtApps = 4
3635

3736
1_name = Communicator
3837
1_start_delay = 10
39-
1_ping_delay = 60
40-
1_ping_interval = 20
38+
1_heartbeat_delay = 60
39+
1_heartbeat_interval = 20
4140
1_cmd = /usr/bin/python test_child.py 1 crash
4241

4342
2_name = Bot
4443
2_start_delay = 20
45-
2_ping_delay = 90
46-
2_ping_interval = 30
47-
2_cmd = /usr/bin/python test_child.py 2 noping
44+
2_heartbeat_delay = 90
45+
2_heartbeat_interval = 30
46+
2_cmd = /usr/bin/python test_child.py 2 noheartbeat
4847

4948
3_name = Publisher
5049
3_start_delay = 35
51-
3_ping_delay = 70
52-
3_ping_interval = 16
50+
3_heartbeat_delay = 70
51+
3_heartbeat_interval = 16
5352
3_cmd = /usr/bin/python test_child.py 3 crash
5453

5554
4_name = Alert
5655
4_start_delay = 35
57-
4_ping_delay = 130
58-
4_ping_interval = 13
59-
4_cmd = /usr/bin/python test_child.py 4 noping
56+
4_heartbeat_delay = 130
57+
4_heartbeat_interval = 13
58+
4_cmd = /usr/bin/python test_child.py 4 noheartbeat
6059
```
6160

6261
### Fields
6362
- `name` : Name of the application.
6463
- `start_delay` : Delay in seconds before starting the application.
65-
- `ping_delay` : Time in seconds to wait before expecting a ping from the application.
66-
- `ping_interval` : Maximum time period in seconds between pings.
64+
- `heartbeat_delay` : Time in seconds to wait before expecting a heartbeat from the application.
65+
- `heartbeat_interval` : Maximum time period in seconds between heartbeats.
6766
- `cmd` : Command to start the application.
6867

6968
## Heartbeat Message
@@ -286,18 +285,18 @@ The application generates log files to monitor the status of each managed proces
286285
Statistics for App 2 Publisher:
287286
Started at: 2024-06-07 20:17:10
288287
Crashed at: Never
289-
Ping reset at: Never
288+
Heartbeat reset at: Never
290289
Start count: 7
291290
Crash count: 0
292-
Ping reset count: 0
293-
Ping count: 11937
294-
Ping count old: 15455
295-
Average first ping time: 105 seconds
296-
Maximum first ping time: 107 seconds
297-
Minimum first ping time: 104 seconds
298-
Average ping time: 102 seconds
299-
Maximum ping time: 110 seconds
300-
Minimum ping time: 102 seconds
291+
Heartbeat reset count: 0
292+
Heartbeat count: 11937
293+
Heartbeat count old: 15455
294+
Average first heartbeat time: 105 seconds
295+
Maximum first heartbeat time: 107 seconds
296+
Minimum first heartbeat time: 104 seconds
297+
Average heartbeat time: 102 seconds
298+
Maximum heartbeat time: 110 seconds
299+
Minimum heartbeat time: 102 seconds
301300
Magic: A50FAA55
302301
```
303302

@@ -337,7 +336,6 @@ Use the provided `run.sh` script to start the Process Watchdog application. This
337336
Or just `./run.sh &` which is recommended.
338337

339338
## TODO
340-
- Replace ping with heartbeat in the code and .ini
341339
- Redesign the apps.c
342340
- Add CPU & RAM usage to the statistics
343341
- Create easy-to-use heartbeat libraries

config.ini

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ udp_port = 12345
44
nWdtApps = 4
55
1_name = Communicator
66
1_start_delay = 10
7-
1_ping_delay = 60
8-
1_ping_interval = 20
7+
1_heartbeat_delay = 60
8+
1_heartbeat_interval = 20
99
1_cmd = /usr/bin/python test_child.py 1 crash
1010
2_name = Bot
1111
2_start_delay = 20
12-
2_ping_delay = 90
13-
2_ping_interval = 30
14-
2_cmd = /usr/bin/python test_child.py 2 noping
12+
2_heartbeat_delay = 90
13+
2_heartbeat_interval = 30
14+
2_cmd = /usr/bin/python test_child.py 2 noheartbeat
1515
3_name = Publisher
1616
3_start_delay = 35
17-
3_ping_delay = 70
18-
3_ping_interval = 16
17+
3_heartbeat_delay = 70
18+
3_heartbeat_interval = 16
1919
3_cmd = /usr/bin/python test_child.py 3 crash
2020
4_name = Alert
2121
4_start_delay = 35
22-
4_ping_delay = 130
23-
4_ping_interval = 13
24-
4_cmd = /usr/bin/python test_child.py 4 noping
22+
4_heartbeat_delay = 130
23+
4_heartbeat_interval = 13
24+
4_cmd = /usr/bin/python test_child.py 4 noheartbeat

src/apps.c

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
The application ensures high reliability and availability by continuously monitoring and
1111
restarting processes as necessary. It also logs various statistics about the monitored
12-
processes, including start times, crash times, and ping intervals.
12+
processes, including start times, crash times, and heartbeat intervals.
1313
1414
@date 2023-01-01
1515
@version 1.0
@@ -47,15 +47,15 @@ typedef struct
4747
{
4848
// In the ini file
4949
int start_delay; /**< Delay in seconds before starting the application. */
50-
int ping_delay; /**< Time in seconds to wait before expecting a ping from the application. */
51-
int ping_interval; /**< Maximum time period in seconds between pings. */
50+
int heartbeat_delay; /**< Time in seconds to wait before expecting a heartbeat from the application. */
51+
int heartbeat_interval; /**< Maximum time period in seconds between heartbeats. */
5252
char name[MAX_APP_NAME_LENGTH]; /**< Name of the application. */
5353
char cmd[MAX_APP_CMD_LENGTH]; /**< Command to start the application. */
5454
// Not in the ini file
5555
bool started; /**< Flag indicating whether the application has been started. */
56-
bool first_ping; /**< Flag indicating whether the application has sent its first ping. */
56+
bool first_heartbeat; /**< Flag indicating whether the application has sent its first heartbeat. */
5757
int pid; /**< Process ID of the application. */
58-
time_t last_ping; /**< Time when the last ping was received from the application. */
58+
time_t last_heartbeat; /**< Time when the last heartbeat was received from the application. */
5959
} Application_t;
6060

6161
static Application_t apps[MAX_APPS]; /**< Array of Application_t structures representing applications defined in the ini file. */
@@ -71,23 +71,23 @@ static int ini_index; /**< Index used to read an array in the ini file. */
7171
void print_app(int i)
7272
{
7373
LOGN("## Application info [%d]", i);
74-
LOGN("%d- name : %s", i, apps[i].name);
75-
LOGN("%d- start_delay : %d", i, apps[i].start_delay);
76-
LOGN("%d- ping_delay : %d", i, apps[i].ping_delay);
77-
LOGN("%d- ping_interval: %d", i, apps[i].ping_interval);
78-
LOGN("%d- cmd : %s", i, apps[i].cmd);
79-
LOGN("%d- started : %d", i, apps[i].started);
80-
LOGN("%d- first_ping : %d", i, apps[i].first_ping);
81-
LOGN("%d- pid : %d", i, apps[i].pid);
82-
LOGN("%d- last_ping : %d", i, apps[i].last_ping);
74+
LOGN("%d- name : %s", i, apps[i].name);
75+
LOGN("%d- start_delay : %d", i, apps[i].start_delay);
76+
LOGN("%d- heartbeat_delay : %d", i, apps[i].heartbeat_delay);
77+
LOGN("%d- heartbeat_interval: %d", i, apps[i].heartbeat_interval);
78+
LOGN("%d- cmd : %s", i, apps[i].cmd);
79+
LOGN("%d- started : %d", i, apps[i].started);
80+
LOGN("%d- first_heartbeat : %d", i, apps[i].first_heartbeat);
81+
LOGN("%d- pid : %d", i, apps[i].pid);
82+
LOGN("%d- last_heartbeat : %d", i, apps[i].last_heartbeat);
8383
}
8484

8585
//------------------------------------------------------------------
8686

87-
void update_ping_time(int i)
87+
void update_heartbeat_time(int i)
8888
{
89-
apps[i].last_ping = time(NULL);
90-
LOGD("Ping time updated for %s", apps[i].name);
89+
apps[i].last_heartbeat = time(NULL);
90+
LOGD("Heartbeat time updated for %s", apps[i].name);
9191
}
9292

9393
int find_pid(int pid)
@@ -103,39 +103,39 @@ int find_pid(int pid)
103103
return -1;
104104
}
105105

106-
time_t get_ping_time(int i)
106+
time_t get_heartbeat_time(int i)
107107
{
108108
time_t t = time(NULL);
109-
return t - apps[i].last_ping;
109+
return t - apps[i].last_heartbeat;
110110
}
111111

112112
bool is_timeup(int i)
113113
{
114114
bool ret = false;
115115
time_t t = time(NULL);
116116

117-
if(t < apps[i].last_ping)
117+
if(t < apps[i].last_heartbeat)
118118
{
119-
update_ping_time(i);
119+
update_heartbeat_time(i);
120120
}
121121

122-
if(t - apps[i].last_ping >= (apps[i].first_ping ? apps[i].ping_interval : apps[i].ping_delay))
122+
if(t - apps[i].last_heartbeat >= (apps[i].first_heartbeat ? apps[i].heartbeat_interval : apps[i].heartbeat_delay))
123123
{
124124
ret = true;
125-
LOGD("Ping time up for %s", apps[i].name);
125+
LOGD("Heartbeat time up for %s", apps[i].name);
126126
}
127127

128128
return ret;
129129
}
130130

131-
void set_first_ping(int i)
131+
void set_first_heartbeat(int i)
132132
{
133-
apps[i].first_ping = true;
133+
apps[i].first_heartbeat = true;
134134
}
135135

136-
bool get_first_ping(int i)
136+
bool get_first_heartbeat(int i)
137137
{
138-
return apps[i].first_ping;
138+
return apps[i].first_heartbeat;
139139
}
140140

141141
//------------------------------------------------------------------
@@ -218,18 +218,18 @@ static int handler(void *user, const char *section, const char *name, const char
218218
apps[ini_index].start_delay = atoi(value);
219219
}
220220

221-
SECTION(ini_index, "ping_delay");
221+
SECTION(ini_index, "heartbeat_delay");
222222

223223
if(MATCH(_section, b))
224224
{
225-
apps[ini_index].ping_delay = atoi(value);
225+
apps[ini_index].heartbeat_delay = atoi(value);
226226
}
227227

228-
SECTION(ini_index, "ping_interval");
228+
SECTION(ini_index, "heartbeat_interval");
229229

230230
if(MATCH(_section, b))
231231
{
232-
apps[ini_index].ping_interval = atoi(value);
232+
apps[ini_index].heartbeat_interval = atoi(value);
233233
}
234234

235235
SECTION(ini_index, "cmd"); // this always must be the last one
@@ -332,10 +332,10 @@ void start_application(int i)
332332
{
333333
// Parent process
334334
apps[i].started = true;
335-
apps[i].first_ping = false;
335+
apps[i].first_heartbeat = false;
336336
apps[i].pid = pid;
337337
LOGI("Process %s started (PID %d): %s", apps[i].name, apps[i].pid, apps[i].cmd);
338-
update_ping_time(i);
338+
update_heartbeat_time(i);
339339
}
340340
}
341341

@@ -426,7 +426,7 @@ void kill_application(int i)
426426
if(killed)
427427
{
428428
apps[i].started = false;
429-
apps[i].first_ping = false;
429+
apps[i].first_heartbeat = false;
430430
apps[i].pid = 0;
431431
}
432432
}
@@ -454,8 +454,8 @@ void restart_application(int i)
454454
}
455455
else
456456
{
457-
// Update the last_ping time to prevent immediate restart
458-
update_ping_time(i);
457+
// Update the last_heartbeat time to prevent immediate restart
458+
update_heartbeat_time(i);
459459
// Log that the application has been successfully restarted
460460
LOGI("Process %s restarted successfully", apps[i].name);
461461
}

src/apps.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
The application ensures high reliability and availability by continuously monitoring and
1111
restarting processes as necessary. It also logs various statistics about the monitored
12-
processes, including start times, crash times, and ping intervals.
12+
processes, including start times, crash times, and heartbeat intervals.
1313
1414
@date 2023-01-01
1515
@version 1.0
@@ -46,11 +46,11 @@
4646
void print_app(int i);
4747

4848
/**
49-
@brief Updates the last ping time for the specified application.
49+
@brief Updates the last heartbeat time for the specified application.
5050
5151
@param i Index of the application.
5252
*/
53-
void update_ping_time(int i);
53+
void update_heartbeat_time(int i);
5454

5555
/**
5656
@brief Finds the index of an application with the specified process ID.
@@ -61,35 +61,35 @@ void update_ping_time(int i);
6161
int find_pid(int pid);
6262

6363
/**
64-
@brief Gets the elapsed time since the last ping received from the specified application.
64+
@brief Gets the elapsed time since the last heartbeat received from the specified application.
6565
6666
@param i Index of the application.
6767
@return Elapsed time in seconds.
6868
*/
69-
time_t get_ping_time(int i);
69+
time_t get_heartbeat_time(int i);
7070

7171
/**
72-
@brief Checks if it is time to expect a ping from the specified application.
72+
@brief Checks if it is time to expect a heartbeat from the specified application.
7373
7474
@param i Index of the application.
75-
@return true if it is time to expect a ping, false otherwise.
75+
@return true if it is time to expect a heartbeat, false otherwise.
7676
*/
7777
bool is_timeup(int i);
7878

7979
/**
80-
@brief Sets the flag indicating that the specified application has sent its first ping.
80+
@brief Sets the flag indicating that the specified application has sent its first heartbeat.
8181
8282
@param i Index of the application.
8383
*/
84-
void set_first_ping(int i);
84+
void set_first_heartbeat(int i);
8585

8686
/**
87-
@brief Gets the flag indicating whether the specified application has sent its first ping.
87+
@brief Gets the flag indicating whether the specified application has sent its first heartbeat.
8888
8989
@param i Index of the application.
90-
@return true if the application has sent its first ping, false otherwise.
90+
@return true if the application has sent its first heartbeat, false otherwise.
9191
*/
92-
bool get_first_ping(int i);
92+
bool get_first_heartbeat(int i);
9393

9494
/**
9595
@brief Sets the path to the ini file.

src/filecmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
The application ensures high reliability and availability by continuously monitoring and
1111
restarting processes as necessary. It also logs various statistics about the monitored
12-
processes, including start times, crash times, and ping intervals.
12+
processes, including start times, crash times, and heartbeat intervals.
1313
1414
@date 2023-01-01
1515
@version 1.0

0 commit comments

Comments
 (0)