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 REQUEST] Minimal Output #72

Closed
Jacksaur opened this issue Feb 26, 2024 · 6 comments
Closed

[FEATURE REQUEST] Minimal Output #72

Jacksaur opened this issue Feb 26, 2024 · 6 comments

Comments

@Jacksaur
Copy link

This script looks like the perfect solution for what I'm trying to put together.
I have a Grafana Dashboard that lists details about my running containers and host system. I'd like to have a panel that counts how many containers currently have updates available to download.
Dockcheck handles the majority of the work, but it's difficult to pull the data I need out of it after. I can't seem to be able to grab anything with Echo or Awk, though my Bash knowledge is extremely beginner. Would it be possible to add a Minimal Output mode, that just prints the containers with updates available to stdout, and nothing else?

@mag37
Copy link
Owner

mag37 commented Feb 27, 2024

I'm not entirely sure what your wished output should look like. But in the extras directory there's a file called dc_brief.sh, with some modifications that might give you the result you need?

I'll gladly try to help you out.

Maybe this is a start? Wont show progress, just the list of containers with updates available when done.

#!/usr/bin/env bash
### If not in PATH, set full path. Else just "regctl"
regbin="regctl"
### options to allow exclude:
while getopts "e:" options; do
  case "${options}" in
    e) Exclude=${OPTARG} ;;
    *) exit 0 ;;
  esac
done
shift "$((OPTIND-1))"
### Create array of excludes
IFS=',' read -r -a Excludes <<< "$Exclude" ; unset IFS
SearchName="$1"
for i in $(docker ps --filter "name=$SearchName" --format '{{.Names}}') ; do
  for e in "${Excludes[@]}" ; do [[ "$i" == "$e" ]] && continue 2 ; done
  RepoUrl=$(docker inspect "$i" --format='{{.Config.Image}}')
  LocalHash=$(docker image inspect "$RepoUrl" --format '{{.RepoDigests}}')
  ### Checking for errors while setting the variable:
  if RegHash=$($regbin image digest --list "$RepoUrl" 2>/dev/null) ; then
    if [[ "$LocalHash" = *"$RegHash"* ]] ; then NoUpdates+=("$i"); else GotUpdates+=("$i"); fi
  else
    GotErrors+=("$i")
  fi
done
### Sort arrays alphabetically
IFS=$'\n'
NoUpdates=($(sort <<<"${NoUpdates[*]}"))
GotUpdates=($(sort <<<"${GotUpdates[*]}"))
GotErrors=($(sort <<<"${GotErrors[*]}"))
unset IFS

if [[ -n ${GotUpdates[*]} ]] ; then
   printf "%s\n" "${GotUpdates[@]}"
fi

Or you could write them to a file if you'd like, just replace the last bit printf "%s\n" "${GotUpdates[@]}" with printf "%s\n" "${GotUpdates[@]}" > FileName.txt

@yoyoma2
Copy link
Contributor

yoyoma2 commented Feb 28, 2024

Couldn't he use notify_generic.sh and tweak it to print exactly what he wants in an easy to grep form?

@Jacksaur
Copy link
Author

In truth, I didn't notice the Extras folder entirely :V
I'm not extremely well versed in Linux still, so apologies for the vagueness of my request. I use Telegraf to collect metrics which it then pushes to my Grafana dashboard. It has an option to run scripts and send their output as metrics, and I'd only need the amount of containers with updates available for what I want to display. So an ideal output would literally just be the number of containers with updates available, and nothing else at all, for easier parsing.

DC_Brief seems like it'd be the easiest option for this, but it doesn't seem to work on my system. Just outputs the containers that cannot update due to errors, and nothing after that. I tried your provided script as well, but that didn't output anything at all.

Renaming notify_generic to just notify.sh doesn't seem to apply it properly for me either, it never prints anything more than Dockcheck does.

@yoyoma2
Copy link
Contributor

yoyoma2 commented Feb 28, 2024

Renaming notify_generic to just notify.sh doesn't seem to apply it properly for me either, it never prints anything more than Dockcheck does.

Don't forget the -i to turn on information notification. If you have no outdated containers the notification doesn't get called at all.

sudo dockcheck.sh -n -s -i

Generic notification addon:
The following 1 docker containers on TestHost need to be updated:
pihole-nightly

I tweaked the print statement to include the number of outdated containers as follows. If you improve the notify_generic.sh file the dev may merge your changes.
printf "\nThe following ${#Updates[@]} docker containers on %s need to be updated:\n%s\n" "$FromHost" "$UpdToString"

@mag37
Copy link
Owner

mag37 commented Feb 28, 2024

DC_Brief seems like it'd be the easiest option for this, but it doesn't seem to work on my system. Just outputs the containers that cannot update due to errors, and nothing after that. I tried your provided script as well, but that didn't output anything at all.

Well that sounds like it's probably due to not editing the regbin="regctl" line. Which has to correspond to the regctl-binary - either by full path, alias or that the binary is in placed $PATH
So for example if you've pasted the text above to a script called dcb.sh and placed the regctl binary in the same path, just edit the script to say regbin=./regctl or preferably full path /path/to/file/regctl.

Renaming notify_generic to just notify.sh doesn't seem to apply it properly for me either, it never prints anything more than Dockcheck does.

Odd - even when you run the script with the flags -ni as @yoyoma2 suggested?

Also if I understand you correctly you'd just like the number of containers as a plain number as the result?
Like

./dcb.sh
5

If you use the notify.sh you would still get the output of the script, so that's not very clean. But could be done with just pasting this code in a new file called notify.sh and running the regular script with -ni flags

send_notification() {
  Updates=("$@")
  UpdCount="${#Updates[@]}"
  printf "\n%s\n" "$UpdCount"

But to just get the number as an output, I'd use what I pasted above (a modification of the dc_brief.sh) and just add a #-sign to the array printed.
So on the 2nd to last row it would be: printf "%s\n" "${#GotUpdates[@]}"
(after you've sorted the regbin-bit).

@Jacksaur
Copy link
Author

Ah, christ. That's embarassing. I forgot that the main script handled regctl automatically.
dc_brief outputs exactly as I desire with your suggested edits. Thanks for putting up with me fellas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants