Как определить, когда служба была запущена и остановлена, используя журнал systemd

by itisgood

Определим, когда служба была запущена и остановлена, используя журнал systemd.

Обратите внимание, тут используется реализацию AWK проекта GNU.

$ sudo journalctl --unit rsyslog -o export --output-fields=INVOCATION_ID | gawk '
BEGIN { 
  RS = "\n\n"
  FS = "\n"
  
  True  = 1
  False = 0
  
  invocation_id_prev      = ""
  realtime_timestamp_prev = ""
  boot_id_prev            = ""
  first_entry             = True

  printf "%32s %34s\n", "invocation id", "start & stop datetime"
} 

{
  for(field = 1; field <= NF; field++) {
    separator_index = index($field, "=")
    key             = substr($field, 0, separator_index - 1)
    value           = substr($field,    separator_index + 1)
        
    switch(key) {
      case "INVOCATION_ID":
        invocation_id = value
        break
      case "__REALTIME_TIMESTAMP":
        realtime_timestamp = value
        break
      case "_BOOT_ID":
        boot_id = value
        break
    }	
  }

  if (invocation_id_prev != invocation_id) {
    if(invocation_id_prev != "" && invocation_id != "") {
      if(first_entry != True) {    
        printf "%32s till %29s\n", " ", strftime("%a %b %e %H:%M:%S %Z %Y", realtime_timestamp_prev/1e6)
      } else {
        first_entry = False
      }
      printf "%32s from %29s ", invocation_id, strftime("%a %b %e %H:%M:%S %Z %Y", realtime_timestamp/1e6)
      if(boot_id_prev != boot_id) {
        print "[reboot detected]"
      } else {
        print ""
      }      
    }
  }
			
  invocation_id_prev = invocation_id
  realtime_timestamp_prev  = realtime_timestamp	
  boot_id_prev = boot_id
}'

Образец вывода для данного сервиса.

      invocation id              start & stop datetime
26ddb2a786104de79c96ad068c58d4fa from Sun May 23 23:23:55 CEST 2021 [reboot detected]
                                 till Sun May 23 23:27:30 CEST 2021
d46b5d1641af4a7aba33bf4f72c93e51 from Sun May 23 23:29:27 CEST 2021 [reboot detected]
                                 till Fri Jun 11 10:56:49 CEST 2021
acd070c0929046ee968d21de3803f247 from Fri Jun 11 10:59:28 CEST 2021 [reboot detected]
                                 till Sat Jun 19 10:42:50 CEST 2021
1397072db60a412bb685fa7ff0006059 from Sat Jun 19 10:46:16 CEST 2021 [reboot detected]
                                 till Sat Jun 19 10:56:52 CEST 2021
b9c170e9a1c54cce9fabe1af9124203a from Sat Jun 19 10:58:51 CEST 2021 [reboot detected]
                                 till Thu Jun 24 23:25:53 CEST 2021
c6f5e21845f64c4ba765eda52aff2bd2 from Thu Jun 24 23:27:44 CEST 2021 [reboot detected]
                                 till Fri Jul  2 15:26:56 CEST 2021
b3499dee6a094184b762828bf7734f46 from Fri Jul  2 15:28:49 CEST 2021 [reboot detected]
                                 till Fri Jul  2 16:17:50 CEST 2021
1621f53e48c84517afa8dff828f6782a from Fri Jul  2 16:17:50 CEST 2021 
                                 till Fri Jul  2 16:32:27 CEST 2021
eaf5300d21c048d38182ddaafdf49e45 from Fri Jul  2 16:34:20 CEST 2021 [reboot detected]
                                 till Fri Jul  2 16:37:05 CEST 2021
0c138a60583f46af96b9c8a64e4614da from Fri Jul  2 16:39:03 CEST 2021 [reboot detected]
                                 till Fri Jul  2 16:40:04 CEST 2021
4c564c9368b04b749cb901615077f59d from Fri Jul  2 16:42:05 CEST 2021 [reboot detected]
                                 till Fri Jul  2 16:47:05 CEST 2021
2a276cd2039a4f739dbc5a4940e224b9 from Fri Jul  2 16:51:10 CEST 2021 [reboot detected]
                                 till Fri Jul  2 17:01:50 CEST 2021
5ffa5f03ecb74fafb1d36493071c947c from Fri Jul  2 17:01:50 CEST 2021 
                                 till Fri Jul  2 17:02:46 CEST 2021
db55d51d19d04e5aaf4c6e6896156a27 from Fri Jul  2 17:04:38 CEST 2021 [reboot detected]
                                 till Sun Jul 18 00:00:07 CEST 2021
029a1a48fb4d4be3ba1e658d529c30ae from Sun Jul 18 14:13:19 CEST 2021 [reboot detected]
                                 till Sun Jul 18 14:15:53 CEST 2021
469b93453a0d4b64bc1ada6f03029f27 from Sun Jul 18 14:17:54 CEST 2021 [reboot detected]
                                 till Sun Jul 25 00:10:23 CEST 2021
4530f1769ef547dd830239a60188f260 from Thu Jul 29 17:59:26 CEST 2021 [reboot detected]
                                 till Sun Sep  5 00:00:28 CEST 2021
335bff16149745de932d93fb2367aa74 from Mon Sep  6 21:39:14 CEST 2021 [reboot detected]
                                 till Mon Sep  6 21:39:14 CEST 2021
2c3fdb08461548289f447f86cf6136a9 from Tue Sep  7 20:26:04 CEST 2021 [reboot detected]
                                 till Tue Sep  7 20:26:04 CEST 2021
3ef6ae71ee0b4cc38432ab063b484c39 from Thu Sep  9 00:19:27 CEST 2021 [reboot detected]
                                 till Fri Sep 10 13:30:22 CEST 2021
a0e015361aa640e28d3b0d74a005dfe9 from Sat Sep 11 00:00:57 CEST 2021 [reboot detected]
                                 till Sat Sep 18 00:32:57 CEST 2021
1cda07da3dba49c7bef48cd7304917e5 from Sat Sep 18 00:34:50 CEST 2021 [reboot detected]

 

You may also like

Leave a Comment