Skip to content

Commit 0a298d9

Browse files
moriyoshidenji
authored andcommitted
Show when the first failure happens in the status matrix.
1 parent e8d2f01 commit 0a298d9

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

ngx_http_upstream_check_module.c

+54-8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ typedef struct {
7878
ngx_pid_t owner;
7979

8080
ngx_msec_t access_time;
81+
ngx_msec_t last_event_time;
8182

8283
ngx_uint_t fall_count;
8384
ngx_uint_t rise_count;
@@ -805,6 +806,12 @@ static ngx_check_status_command_t ngx_check_status_commands[] = {
805806
};
806807

807808

809+
static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
810+
static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
811+
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
812+
};
813+
814+
808815
static ngx_uint_t ngx_http_upstream_check_shm_generation = 0;
809816
static ngx_http_upstream_check_peers_t *check_peers_ctx = NULL;
810817

@@ -2583,8 +2590,8 @@ ngx_http_upstream_check_status_update(ngx_http_upstream_check_peer_t *peer,
25832590
} else {
25842591
peer->shm->rise_count++;
25852592
peer->shm->fall_count = 0;
2586-
if (peer->shm->down && peer->shm->rise_count >= ucscf->rise_count) {
2587-
peer->shm->down = 0;
2593+
if (peer->shm->rise_count >= ucscf->rise_count && peer->shm->down == 1 && ngx_atomic_cmp_set(&peer->shm->down, 1, 0)) {
2594+
peer->shm->last_event_time = ngx_current_msec;
25882595
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
25892596
"enable check peer: %V, raise: %ui/%ui",
25902597
&peer->check_peer_addr->name,
@@ -2594,8 +2601,8 @@ ngx_http_upstream_check_status_update(ngx_http_upstream_check_peer_t *peer,
25942601
} else {
25952602
peer->shm->rise_count = 0;
25962603
peer->shm->fall_count++;
2597-
if (!peer->shm->down && peer->shm->fall_count >= ucscf->fall_count) {
2598-
peer->shm->down = 1;
2604+
if (peer->shm->fall_count >= ucscf->fall_count && peer->shm->down == 0 && ngx_atomic_cmp_set(&peer->shm->down, 0, 1)) {
2605+
peer->shm->last_event_time = ngx_current_msec;
25992606
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
26002607
"disable check peer: %V, fall: %ui/%ui",
26012608
&peer->check_peer_addr->name,
@@ -2952,11 +2959,22 @@ ngx_http_upstream_check_status_html_format(ngx_buf_t *b,
29522959
" <th>Fall counts</th>\n"
29532960
" <th>Check type</th>\n"
29542961
" <th>Check port</th>\n"
2962+
" <th>Uptime or First failure since</th>\n"
2963+
" <th>Elapse in seconds</th>\n"
29552964
" </tr>\n",
29562965
count, ngx_http_upstream_check_shm_generation);
29572966

29582967
for (i = 0; i < peers->peers.nelts; i++) {
29592968

2969+
u_char event_time_str[sizeof("Mon 28 Sep 1970 06:00:00 UTC")];
2970+
ngx_tm_t tm;
2971+
ngx_gmtime(peer[i].shm->last_event_time / 1000, &tm);
2972+
(void) ngx_sprintf(event_time_str,
2973+
"%s %02d %s %4d %02d:%02d:%02d UTC",
2974+
week[tm.ngx_tm_wday], tm.ngx_tm_mday,
2975+
months[tm.ngx_tm_mon - 1], tm.ngx_tm_year,
2976+
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
2977+
29602978
if (flag & NGX_CHECK_STATUS_DOWN) {
29612979

29622980
if (!peer[i].shm->down) {
@@ -2980,6 +2998,8 @@ ngx_http_upstream_check_status_html_format(ngx_buf_t *b,
29802998
" <td>%ui</td>\n"
29812999
" <td>%V</td>\n"
29823000
" <td>%ui</td>\n"
3001+
" <td>%s</td>\n"
3002+
" <td>%d</td>\n"
29833003
" </tr>\n",
29843004
peer[i].shm->down ? " bgcolor=\"#FF0000\"" : "",
29853005
i,
@@ -2989,7 +3009,9 @@ ngx_http_upstream_check_status_html_format(ngx_buf_t *b,
29893009
peer[i].shm->rise_count,
29903010
peer[i].shm->fall_count,
29913011
&peer[i].conf->check_type_conf->name,
2992-
peer[i].conf->port);
3012+
peer[i].conf->port,
3013+
event_time_str,
3014+
(int)((ngx_current_msec - peer[i].shm->last_event_time) / 1000));
29933015
}
29943016

29953017
b->last = ngx_snprintf(b->last, b->end - b->last,
@@ -3008,6 +3030,15 @@ ngx_http_upstream_check_status_csv_format(ngx_buf_t *b,
30083030
peer = peers->peers.elts;
30093031
for (i = 0; i < peers->peers.nelts; i++) {
30103032

3033+
u_char event_time_str[sizeof("Mon 28 Sep 1970 06:00:00 UTC")];
3034+
ngx_tm_t tm;
3035+
ngx_gmtime(peer[i].shm->last_event_time / 1000, &tm);
3036+
(void) ngx_sprintf(event_time_str,
3037+
"%s %02d %s %4d %02d:%02d:%02d UTC",
3038+
week[tm.ngx_tm_wday], tm.ngx_tm_mday,
3039+
months[tm.ngx_tm_mon - 1], tm.ngx_tm_year,
3040+
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
3041+
30113042
if (flag & NGX_CHECK_STATUS_DOWN) {
30123043

30133044
if (!peer[i].shm->down) {
@@ -3022,15 +3053,17 @@ ngx_http_upstream_check_status_csv_format(ngx_buf_t *b,
30223053
}
30233054

30243055
b->last = ngx_snprintf(b->last, b->end - b->last,
3025-
"%ui,%V,%V,%s,%ui,%ui,%V,%ui\n",
3056+
"%ui,%V,%V,%s,%ui,%ui,%V,%ui,%s,%d\n",
30263057
i,
30273058
peer[i].upstream_name,
30283059
&peer[i].peer_addr->name,
30293060
peer[i].shm->down ? "down" : "up",
30303061
peer[i].shm->rise_count,
30313062
peer[i].shm->fall_count,
30323063
&peer[i].conf->check_type_conf->name,
3033-
peer[i].conf->port);
3064+
peer[i].conf->port,
3065+
event_time_str,
3066+
(int)((ngx_current_msec - peer[i].shm->last_event_time) / 1000));
30343067
}
30353068
}
30363069

@@ -3073,6 +3106,15 @@ ngx_http_upstream_check_status_json_format(ngx_buf_t *b,
30733106
last = peers->peers.nelts - 1;
30743107
for (i = 0; i < peers->peers.nelts; i++) {
30753108

3109+
u_char event_time_str[sizeof("Mon 28 Sep 1970 06:00:00 UTC")];
3110+
ngx_tm_t tm;
3111+
ngx_gmtime(peer[i].shm->last_event_time / 1000, &tm);
3112+
(void) ngx_sprintf(event_time_str,
3113+
"%s %02d %s %4d %02d:%02d:%02d UTC",
3114+
week[tm.ngx_tm_wday], tm.ngx_tm_mday,
3115+
months[tm.ngx_tm_mon - 1], tm.ngx_tm_year,
3116+
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
3117+
30763118
if (flag & NGX_CHECK_STATUS_DOWN) {
30773119

30783120
if (!peer[i].shm->down) {
@@ -3094,7 +3136,9 @@ ngx_http_upstream_check_status_json_format(ngx_buf_t *b,
30943136
"\"rise\": %ui, "
30953137
"\"fall\": %ui, "
30963138
"\"type\": \"%V\", "
3097-
"\"port\": %ui}"
3139+
"\"port\": %ui, "
3140+
"\"since\": \"%s\", "
3141+
"\"elapse\": %d }"
30983142
"%s\n",
30993143
i,
31003144
peer[i].upstream_name,
@@ -3104,6 +3148,8 @@ ngx_http_upstream_check_status_json_format(ngx_buf_t *b,
31043148
peer[i].shm->fall_count,
31053149
&peer[i].conf->check_type_conf->name,
31063150
peer[i].conf->port,
3151+
event_time_str,
3152+
(int)((ngx_current_msec - peer[i].shm->last_event_time) / 1000),
31073153
(i == last) ? "" : ",");
31083154
}
31093155

0 commit comments

Comments
 (0)