xref: /php-src/sapi/fpm/fpm/fpm_systemd.c (revision e2a5428c)
1 #include "fpm_config.h"
2 
3 #include <sys/types.h>
4 #include <systemd/sd-daemon.h>
5 
6 #include "fpm.h"
7 #include "fpm_clock.h"
8 #include "fpm_worker_pool.h"
9 #include "fpm_scoreboard.h"
10 #include "zlog.h"
11 #include "fpm_systemd.h"
12 
13 
fpm_systemd(void)14 static void fpm_systemd(void)
15 {
16 	static unsigned long int last=0;
17 	struct fpm_worker_pool_s *wp;
18 	unsigned long int requests=0, slow_req=0;
19 	int active=0, idle=0;
20 
21 
22 	for (wp = fpm_worker_all_pools; wp; wp = wp->next) {
23 		if (wp->scoreboard) {
24 			active   += wp->scoreboard->active;
25 			idle     += wp->scoreboard->idle;
26 			requests += wp->scoreboard->requests;
27 			slow_req += wp->scoreboard->slow_rq;
28 		}
29 	}
30 
31 /*
32 	zlog(ZLOG_DEBUG, "systemd %s (Processes active:%d, idle:%d, Requests:%lu, slow:%lu, Traffic:%.3greq/sec)",
33 			fpm_global_config.systemd_watchdog ? "watchdog" : "heartbeat",
34 			active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / fpm_global_config.systemd_interval);
35 */
36 
37 	if (0 > sd_notifyf(0, "READY=1\n%s"
38 				"STATUS=Processes active: %d, idle: %d, Requests: %lu, slow: %lu, Traffic: %.3greq/sec",
39 				fpm_global_config.systemd_watchdog ? "WATCHDOG=1\n" : "",
40 				active, idle, requests, slow_req, ((float)requests - last) * 1000.0 / fpm_global_config.systemd_interval)) {
41 		zlog(ZLOG_NOTICE, "failed to notify status to systemd");
42 	}
43 
44 	last = requests;
45 }
46 
fpm_systemd_heartbeat(struct fpm_event_s * ev,short which,void * arg)47 void fpm_systemd_heartbeat(struct fpm_event_s *ev, short which, void *arg) /* {{{ */
48 {
49 	static struct fpm_event_s heartbeat;
50 
51 	if (fpm_globals.parent_pid != getpid()) {
52 		return; /* sanity check */
53 	}
54 
55 	if (which == FPM_EV_TIMEOUT) {
56 		fpm_systemd();
57 
58 		return;
59 	}
60 
61 	if (0 > sd_notifyf(0, "READY=1\n"
62 			          "STATUS=Ready to handle connections\n"
63 			          "MAINPID=%lu",
64 			          (unsigned long) getpid())) {
65 		zlog(ZLOG_WARNING, "failed to notify start to systemd");
66 	} else {
67 		zlog(ZLOG_DEBUG, "have notify start to systemd");
68 	}
69 
70 	/* first call without setting which to initialize the timer */
71 	if (fpm_global_config.systemd_interval > 0) {
72 		fpm_event_set_timer(&heartbeat, FPM_EV_PERSIST, &fpm_systemd_heartbeat, NULL);
73 		fpm_event_add(&heartbeat, fpm_global_config.systemd_interval);
74 		zlog(ZLOG_NOTICE, "systemd monitor interval set to %dms", fpm_global_config.systemd_interval);
75 	} else {
76 		zlog(ZLOG_NOTICE, "systemd monitor disabled");
77 	}
78 }
79 /* }}} */
80 
fpm_systemd_conf(void)81 int fpm_systemd_conf(void)
82 {
83 	char *watchdog;
84 	int  interval = 0;
85 
86 	watchdog = getenv("WATCHDOG_USEC");
87 	if (watchdog) {
88 		/* usec to msec, and half the configured delay */
89 		interval = (int)(atol(watchdog) / 2000L);
90 		zlog(ZLOG_DEBUG, "WATCHDOG_USEC=%s, interval=%d", watchdog, interval);
91 	}
92 
93 	if (interval > 1000) {
94 		if (fpm_global_config.systemd_interval > 0) {
95 			zlog(ZLOG_WARNING, "systemd_interval option ignored");
96 		}
97 		zlog(ZLOG_NOTICE, "systemd watchdog configured to %.3gsec", (float)interval / 1000.0);
98 		fpm_global_config.systemd_watchdog = 1;
99 		fpm_global_config.systemd_interval = interval;
100 
101 	} else if (fpm_global_config.systemd_interval < 0) {
102 		/* not set => default value */
103 		fpm_global_config.systemd_interval = FPM_SYSTEMD_DEFAULT_HEARTBEAT;
104 
105 	} else {
106 		/* sec to msec */
107 		fpm_global_config.systemd_interval *= 1000;
108 	}
109 	return 0;
110 }
111