Description

This KB provides an event script that can be used for collection of any outputs on regular basis. 

Symptoms

For example, customer hits an issue with subscribers being not able to connect and Engineering requested collection of shmlogs for further analysis.

Solution

The following event script can be used:

#!/usr/bin/env python3
'''
Copyright 2023 Juniper Networks, Inc. All rights reserved.
Licensed under the Juniper Networks Script Software License (the "License").
You may not use this script file except in compliance with the License, which is located at
http://www.juniper.net/support/legal/scriptlicense/
Unless required by applicable law or otherwise agreed to in writing by the parties,
software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 OR CONDITIONS OF ANY KIND, either express or implied.

Script will collect shmlog entries and statistics and save them in /var/tmp/jtac_data_collection/ folder.

Author: S.Ududenko
Last modified: 15.01.2025
Version: 2.0

Installation as an event script:
1. Save this file as /var/db/scripts/event/jtac_collect_shmlogs.py
File should be owned by root or by a super-user, and only the file owner can have write permissions.
2. Add the following configuration:
set system scripts language python3
set event-options generate-event 4min time-interval 240
set event-options policy save_shmlogs event 4min
set event-options policy save_shmlogs then event-script jtac_collect_shmlogs.py
set event-options event-script file jtac_collect_shmlogs.py python-script-user <user> 
// replace <user> with a valid user that has permissions to execute "show shmlog" CLI commands
3. Additional step if GRES is enabled:
set system scripts synchronize // recommended - to enable automatic copy of scripts to the backup RE on every commit
or "commit synchronize scripts" // to copy scripts to the backup RE during particular commit
or "request system scripts synchronize" // to manually trigger copy of scripts to the backup RE
4. (Optional) To avoid the syslog warning every time this script is executed, checksum can be configured:
"cscript: %USER-4-CSCRIPT_SECURITY_WARNING: unsigned python script '/var/db/scripts/event/jtac_collect_shmlogs.py' without checksum is executed"
set event-options event-script file jtac_collect_shmlogs.py checksum sha-256 <checksum>

If needed, this script can be tested as an op script.
Installation as an op script (additional steps):
1. Save this file as /var/db/scripts/op/jtac_collect_shmlogs.py
2. Add the following configuration:
set system scripts op file jtac_collect_shmlogs.py  
'''

from subprocess import run, PIPE, STDOUT
from datetime import datetime
from junos import Junos_Context
import os
import sys

log_dir = "/var/tmp/jtac_data_collection"
 
re_cmd_list = [
    "show shmlog entries logname all start-from-latest-seconds 300",
    "show shmlog statistics logname all",
    "show subscribers extensive"
]
 
def is_master():
    return "re-master" in Junos_Context

def run_cmd(cmd, check=False):
    output = run(cmd, shell=True, check=check, stdout=PIPE, stderr=STDOUT)
    return output.stdout.decode()

def main():
    if not is_master():
        sys.exit(0)

    if not os.path.exists(f"{log_dir}"):
        os.mkdir(f"{log_dir}")

    ts = datetime.strftime(datetime.now(), "%y%m%d_%H%M%S")
    for cmd in re_cmd_list:
        fname = cmd.replace(" ","_")
        with open(f"{log_dir}/{fname}_{ts}.log", "w") as foo:
            foo.write(f'=== cli -c "{cmd}"\n')
            stdout = run_cmd(f'cli -c "{cmd}"')
            foo.write(stdout)
        run_cmd(f'gzip "{log_dir}/{fname}_{ts}.log"')

if __name__ == '__main__':
    main()

Every 4 minutes outputs from this list will be collected:

re_cmd_list = [
    "show shmlog entries logname all start-from-latest-seconds 300",
    "show shmlog statistics logname all",
    "show subscribers extensive"
]

Output of each command is collected to a separate compressed file:

user@mx> file list /var/tmp/jtac_data_collection/ detail
Jan 15 13:29:43

/var/tmp/jtac_data_collection/:
total blocks: 2536
-rw-r--r--  1 sududenko wheel     23018 Jan 15 13:24 show_shmlog_entries_logname_all_start-from-latest-seconds_300_250115_132458.log.gz
-rw-r--r--  1 sududenko wheel     20146 Jan 15 13:28 show_shmlog_entries_logname_all_start-from-latest-seconds_300_250115_132858.log.gz
-rw-r--r--  1 sududenko wheel     32355 Jan 15 13:24 show_shmlog_statistics_logname_all_250115_132458.log.gz
-rw-r--r--  1 sududenko wheel     32353 Jan 15 13:28 show_shmlog_statistics_logname_all_250115_132858.log.gz
-rw-r--r--  1 sududenko wheel    555974 Jan 15 13:25 show_subscribers_extensive_250115_132458.log.gz
-rw-r--r--  1 sududenko wheel    555974 Jan 15 13:29 show_subscribers_extensive_250115_132858.log.gz

Modification History

2025-01-15 : Article Created