Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sc tail #208

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ install: scripts/sc scripts/scinit scripts/sc_install_defaults target/sc.jar tar
install -m 555 -o qsys scripts/sc ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 555 -o qsys scripts/scinit ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 555 -o qsys scripts/scedit ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 555 -o qsys scripts/sctail ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 555 -o qsys scripts/scopenports ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 555 -o qsys scripts/sc_install_defaults ${INSTALL_ROOT}/QOpenSys/pkgs/bin/
install -m 444 -o qsys target/sc.jar ${INSTALL_ROOT}/QOpenSys/pkgs/lib/sc/sc.jar
Expand Down
92 changes: 92 additions & 0 deletions scripts/sctail
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/QOpenSys/pkgs/bin/bash

if [[ "$LC_ALL" != *UTF-8* ]]; then
# LC_ALL is not set. Set it to EN_US.UTF-8 if present
if [ -e /usr/lib/nls/loc/EN_US.UTF-8 ]; then
export LC_ALL=EN_US.UTF-8
else
# EN_US.UTF-8 is not present. Look for installed UTF-8 locale
INSTALLEDUTF8LOCALE=$(ls -b /usr/lib/nls/loc | grep -E '.UTF-8$' | tail -n 1)
if [[ "" = "$INSTALLEDUTF8LOCALE" ]]; then
>&2 echo "WARNING: Cannot find a UTF-8 locale installed on this system."
else
export LC_ALL=$INSTALLEDUTF8LOCALE
fi
fi
fi

if (($# == 0)); then
echo "usage: sctail [tail OPTION]... <service name>"
exit 0
fi

HELP_MSG=NO

# Set the default arguments for tail
TAIL_ARGS=""

# Initialize variable for service name
SERVICE=""

# Declare an array to hold the arguments and their values
declare -a ARGS=()

# Loop through the command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-c|--bytes|-n|--lines|-s|--sleep-interval|--pid|--max-unchanged-stats)
# If the argument has a value, add it to the array
if [[ $# -gt 1 && "$2" != -* ]]; then
ARGS+=("$1 $2")
shift
else
ARGS+=("$1")
fi
;;
-f|--follow|-F|--retry|-q|--quiet|--silent|-v|--verbose|-z|--zero-terminated|--version)
ARGS+=("$1")
;;
--help)
HELP_MSG=YES
shift # past argument with no value
;;
-*|--*)
echo "sctail: invalid option -- '$1'"
echo "usage: sctail [OPTION]... <service name>"
echo "Try 'sctail --help' for more information."
exit 1
;;
*)

# If the argument doesn't start with a dash, set it as the SERVICE value
SERVICE="$1"
;;
esac
shift
done

if [[ $HELP_MSG == YES ]]; then
echo "sctail usage: sctail [tail OPTION]... <service name]"
exec /QOpenSys/pkgs/bin/tail --help
exit 0
fi


if [[ ${#ARGS[@]} -gt 0 ]]; then
TAIL_ARGS+=" ${ARGS[*]}"
fi


echo "SERVICE: $SERVICE"
echo "TAIL_ARGS: $TAIL_ARGS"

LOG=$($(dirname $0)/sc loginfo $SERVICE | /QOpenSys/pkgs/bin/cut -d ':' -f 2 | awk '{ for (i=1; i<=NF; i++) print $i }')
if [[ -z $LOG ]]; then
echo "No log file found for service $SERVICE"
exit 1
fi

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took another look at the mainline sc code and realize we need to account for the case where sc loginfo provides one or more spooled files

m_logger.println(m_mainService.getName() + ": " + StringUtils.colorizeForTerminal(splf, ColorSchemeConfig.get("INFO")));

As far as I know, there's no already-made utility for displaying spooled files in a terminal, and definitely nothing analagous to tail -f. Some ideas:

  • Just say something like "unable to tail files, here's the spooled files you can look at, though"
  • Build a utility to display spooled files in a terminal. If so, would we include it in SC ? Some other package?
  • run the SQL for SYSTOOLS.SPOOLED_FILE_DATA() through db2util

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, there's no already-made utility for displaying spooled files in a terminal

There is the QSHELL catsplf command, which can be run from PASE environment like this:

qsh -c "catsplf -j 123456/USER/JOB <spoolfilename> <spoolfilenumber>"

But I don't know if it's to cumbersome?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrjorgensen, cool! I think we should use that

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add support for "tailing" spool files. What would be the best way to configure a service that creates spool files to test this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found splf documentation 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By design, I can only view the spool file if the service has stopped (per quickstart demo). If I call catsplf on a running service I get the following output:

catsplf: 001-2003 Error CPF3482 found processing spool file QPRINT, number 1.

but once the service is stopped, I can no longer view the DSPSPLF command from loginfo. sctail works great on running services, since we can access the log file via existing sc commands. In the spool file case, we would need to track the qualified job of the service which would require changing the sc base code unless there is another way to get that service information

Copy link
Collaborator

@chrjorgensen chrjorgensen Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ajshedivy Spool files have (and always have had) the problem that the output is written in blocks and the latest output can not be seen until that block has finished and another block is created. Your problem is the same as when the `DSPSPLF´ command is used - or when the SYSTOOLS.SPOOLED_FILE_DATA() is used.

This makes spool files unsuitable for logs IMHO... writing to streamfiles is much better!

@ThePrez Do you know if it's possible to redirect spool output to a streamfile, while the spool file is open? If not, then I don't think a tail option for spool files can be done...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrjorgensen I don't think so, while the splf is still open :(

echo "LOG: $LOG"

exec /QOpenSys/pkgs/bin/tail $TAIL_ARGS $LOG
ajshedivy marked this conversation as resolved.
Show resolved Hide resolved