Jump to content
Squid

Additional Scripts For User.Scripts Plugin

323 posts in this topic Last Reply

Recommended Posts

I have started to use this script to output my directory structure to a text file and it seems to do what I expect.

 

Could I ask for some assistance to send the contents (log files) to an email address when complete?

 

 

I currently have the system emailing me alerts (such as preclear) and hoping to tie into that function if possible.

 

Suggestions would be very welcome!

 

 

Imp

 

On 12/7/2016 at 3:20 PM, ClunkClunk said:

 

 

Quote

I started this a few months ago, but never got around to really optimizing it and rotating out old listings or compressing them. But hopefully it'll give you a starting point!

 

 


#!/bin/sh

DISKS=8
COUNTER=1
THEDATE=`date +"%Y-%m-%d_%H-%M-%S"`
LOCATION="/boot/trees"

echo " "
echo " "
echo "Starting tree scanning"

while [ $COUNTER -le $DISKS ]
do
echo "Scanning tree for disk$COUNTER/Movies"
tree -h /mnt/disk$COUNTER/Movies >> "$LOCATION/$THEDATE.movies.log"
echo "Scanning tree for disk$COUNTER/TV"
tree -h /mnt/disk$COUNTER/TV >> "$LOCATION/$THEDATE.tv.log"
echo "Incrementing disk counter"
COUNTER=$[$COUNTER+1]
done
echo "Scanning tree for cache/Movies"
tree -h /mnt/cache/Movies >> "$LOCATION/$THEDATE.movies.log"
echo "Scanning tree for cache/TV"
tree -h /mnt/cache/TV >> "$LOCATION/$THEDATE.tv.log"
echo "All done scanning disks. Beginning scanning shares."
echo "Scanning tree for user/Movies"
tree -h /mnt/user/Movies >> "$LOCATION/$THEDATE.movies.log"
echo "Scanning tree for user/TV"
tree -h /mnt/user/TV >> "$LOCATION/$THEDATE.tv.log"
echo " "
echo " "
echo "Complete! Logs stored in $LOCATION"
 

 

 

 

Edited by Impossible
Formatting

Share this post


Link to post
On 1/15/2017 at 11:41 AM, Helmonder said:

if grep -q "Out of memory" /var/log/syslog; then   /usr/local/emhttp/plugins/dynamix/scripts/notify -e "OOM Checker" -s "Checked for OOM in syslog" -d "OOM error found in syslog" -i "alert" fi

 

Not programmer here trying to paste this code in to User Scripts. If I paste all the lines here:

 

if grep -q "Out of memory" /var/log/syslog; then
   /usr/local/emhttp/plugins/dynamix/scripts/notify -e "OOM Checker" -s "Checked for OOM in syslog" -d "OOM error found in syslog" -i "alert" 
fi

I get this in return:

 

/tmp/user.scripts/tmpScripts/Check Out Of Memory errors/script: line 5: syntax error: unexpected end of file

:(

What can I do? Thanks in advance

Share this post


Link to post

if grep -q "Out of memory" /var/log/syslog; then
   /usr/local/emhttp/plugins/dynamix/scripts/notify -e "OOM Checker" -s "Checked for OOM in syslog" -d "OOM error found in syslog" -i "alert" 
 fi

 

Share this post


Link to post
On 6/8/2018 at 7:54 AM, almarma said:

Not programmer here trying to paste this code in to User Scripts. If I paste all the lines here:

 


if grep -q "Out of memory" /var/log/syslog; then
   /usr/local/emhttp/plugins/dynamix/scripts/notify -e "OOM Checker" -s "Checked for OOM in syslog" -d "OOM error found in syslog" -i "alert" 
fi

 

 

Paste your code into a text editor like Notepad++ or EditPad Lite and double check it. There is something extra in there when you copy and paste. Also, convert to UNIX/Linux Line Feed instead of Windows Carriage Return. This is a common problem when copying and pasting Linux scripts using Windows PC's.

Share this post


Link to post
7 minutes ago, GHunter said:

 

Paste your code into a text editor like Notepad++ or EditPad Lite and double check it. There is something extra in there when you copy and paste. Also, convert to UNIX/Linux Line Feed instead of Windows Carriage Return. This is a common problem when copying and pasting Linux scripts using Windows PC's.

 

That might help but if he is pasting it into the edit window of the plugin then that should be taken care of. From the first post in User Scripts plugin thread:

 

Quote

So as to make things easier for people:

  • The script file can be created with any text editor you choose.  DOS line endings will be automatically converted to Linux style endings prior to execution.

 

Also, I question the need for OOM checker just as Helmonder did.

Share this post


Link to post
22 minutes ago, trurl said:

That might help but if he is pasting it into the edit window of the plugin then that should be taken care of. From the first post in User Scripts plugin thread:

 

Ah, OK. Thanks for pointing that out. I know some plugins are good about handling that problem for us and others aren't. Since this is @Squid plugin, I should have known better ?

 

 

Share this post


Link to post
On 6/11/2018 at 1:08 PM, GHunter said:

 

Paste your code into a text editor like Notepad++ or EditPad Lite and double check it. There is something extra in there when you copy and paste. Also, convert to UNIX/Linux Line Feed instead of Windows Carriage Return. This is a common problem when copying and pasting Linux scripts using Windows PC's.

 

Thank you very much. I'm using a Mac but I'll double check it now.

Anyway, I have the awesome "Fix Common Problems" plugin installed and a few days ago it detected itself the OOM error so maybe I don't need the script.

 

Share this post


Link to post

Got a question regarding the Clear Array Disk Script:

Is it normal that it only does 1,5MB/s - 3MB/s in the beginning? Will it get faster?

If it's fucked, can I just abort it? Because with that speed it will take like 2 weeks to get done.

Share this post


Link to post
1 minute ago, GerryGER said:

Got a question regarding the Clear Array Disk Script:

Is it normal that it only does 1,5MB/s - 3MB/s in the beginning? Will it get faster?

If it's fucked, can I just abort it? Because with that speed it will take like 2 weeks to get done.

 

Share this post


Link to post

Get the size of the image (more or less) for each running container.  Useful to see if a massively misconfigured container is ballooning your docker.img.  Does not however consider the log sizes

 

#!/usr/bin/php
<?PHP
require_once("/usr/local/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php");

$DockerTemplates = new DockerTemplates();
$info = $DockerTemplates->getAllInfo();

foreach ($info as $contName=>$cont) {
  if ( $cont['running'] ) {
    echo "$contName  Size: ".rtrim(exec("docker exec $contName du -shx /"),"/")."\n";
  }
}
?>

 

Edited by Squid

Share this post


Link to post
1 hour ago, Squid said:

Get the size of the image (more or less) for each running container.  Useful to see if a massively misconfigured container is ballooning your docker.img.  Does not however consider the log sizes 

 


#!/usr/bin/php
<?PHP
require_once("/usr/local/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php");

$DockerTemplates = new DockerTemplates();
$info = $DockerTemplates->getAllInfo();

foreach ($info as $contName=>$cont) {
  if ( $cont['running'] ) {
    echo "$contName  Size: ".rtrim(exec("docker exec -it $contName du -shx /"),"/")."\n";
  }
}
?>

 

You should remove the "-it"  otherwise you will get a TTY error

Share this post


Link to post
Just now, ffhelllskjdje said:

You should remove the "-it"  otherwise you will get a TTY error

I was just about to post about the tty error - thanks for the fix - working fine now.

Share this post


Link to post
I was just about to post about the tty error - thanks for the fix - working fine now.
Thanks. It's what happens during copies and pasted from a different code base of mine

Sent from my SM-T560NU using Tapatalk

Share this post


Link to post
18 hours ago, Squid said:

Get the size of the image (more or less) for each running container.  Useful to see if a massively misconfigured container is ballooning your docker.img.  Does not however consider the log sizes

 


#!/usr/bin/php
<?PHP
require_once("/usr/local/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php");

$DockerTemplates = new DockerTemplates();
$info = $DockerTemplates->getAllInfo();

foreach ($info as $contName=>$cont) {
  if ( $cont['running'] ) {
    echo "$contName  Size: ".rtrim(exec("docker exec -it $contName du -shx /"),"/")."\n";
  }
}
?>

 

Thank you - very useful

Share this post


Link to post

Get the size of the image (more or less) for each running container.  Useful to see if a massively misconfigured container is ballooning your docker.img.   Now includes log sizes

#!/usr/bin/php
<?PHP
require_once("/usr/local/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php");

$DockerTemplates = new DockerTemplates();
$info = $DockerTemplates->getAllInfo();

foreach ($info as $contName=>$cont) {
  if ( $cont['running'] ) {
    echo "$contName  Size: ".rtrim(exec("docker exec $contName du -shx /"),"/")."   Logs: ";
    echo human_filesize(exec("docker logs $contName 2>&1 | wc -c"),1)."\n";
  }
}

function human_filesize($bytes, $decimals = 2) {
  $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
  $factor = floor((strlen($bytes) - 1) / 3);
  return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
?>

 

Share this post


Link to post
5 hours ago, Squid said:

Get the size of the image (more or less) for each running container.

Can you add stopped as well as running?

Share this post


Link to post
Can you add stopped as well as running?
You can't exec into a stopped container, so the best you can get is the output of docker images which is only the image size , and not any additional files put into the container

Sent from my LG-D852 using Tapatalk

Share this post


Link to post
8 hours ago, Squid said:

You can't exec into a stopped container, so the best you can get is the output of docker images which is only the image size , and not any additional files put into the container

Sent from my LG-D852 using Tapatalk
 

True, but you could walk through all the containers one by one, if it's stopped, start it, measure it, stop it, move on. Notate any failed starts in the size output.

 

Yeah, I know, write my own script if I want, this one already does what it needs to. 😁

Share this post


Link to post
On 9/12/2018 at 7:32 AM, jonathanm said:

Yeah, I know, write my own script if I want, this one already does what it needs to. 😁

 

If you remove the starting "#" on the third line, it will start any stopped containers (and stop them after getting their size)

 

#!/usr/bin/php
<?PHP
#$startStopped = true;

require_once("/usr/local/emhttp/plugins/dynamix.docker.manager/include/DockerClient.php");

$DockerClient = new DockerClient();
$DockerTemplates = new DockerTemplates();

$info = getRunningContainers();

foreach ($info as $contName=>$cont) {
  if ( $startStopped && ! $cont['running'] ) {
    $DockerClient->startContainer($cont['Id']);
  }
  if ( $cont['running'] || $startStopped ) {
    echo "$contName  Size: ".rtrim(exec("docker exec $contName du -shx /"),"/")."   Logs: ";
    echo human_filesize(exec("docker logs $contName 2>&1 | wc -c"),1)."\n";
  }
  if ( ! $cont['running'] && $startStopped ) {
    $DockerClient->stopContainer($cont['Id']);
  }
}

function human_filesize($bytes, $decimals = 2) {
  $size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
  $factor = floor((strlen($bytes) - 1) / 3);
  return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

function getRunningContainers() {
  global $DockerClient, $DockerTemplates;

  $containers = $DockerClient->getDockerContainers();
  $info = $DockerTemplates->getAllInfo();

  foreach ($containers as $container) {
    $info[$container['Name']]['running'] = $container['Running'];
    $info[$container['Name']]['Id'] = $container['Id'];
    $infoTmp[$container['Name']] = $info[$container['Name']];
  }
  return $infoTmp ?: array();
}
?>

 

Share this post


Link to post

Just a heads up for others, direct copy paste from the forums to the GUI user scripts editor may transfer some unprintable characters that keep the script from running. I recommend copying the script into a competent editor first then look through it and sanitize it.

 

@Squid, the error return for an unstartable container is usable, but could be nicer looking.

 

Error response from daemon: Container ed0edd32c5756ff55aba3b1eeac3430c387b42c516b68172971bc9ff7b9517ef is not running
activ-lazylibrarian Size: Logs: 1.3kB

 

It still returns the log size, which is very useful.

Share this post


Link to post
1 minute ago, jonathanm said:

the error return for an unstartable container is usable, but could be nicer looking.

Why do you have an unstartable container?

Share this post


Link to post

you can try changing the docker exec entry to be

docker exec $contName du -shx / 2>/dev/null

Share this post


Link to post
1 minute ago, Squid said:

Why do you have an unstartable container?

Similar or duplicate functions in multiple containers, each mapped to the same port, ensures only one can be started at any given time so they don't both try to modify the files while the other is running. It's actually a fairly useful function.

 

Another usage, I can have a limited permissions krusader container running with very restrictive mapping set to auto start, then when I need more access I stop that container and start the container that's mapped to / rw instead of mapping /mnt/user ro

Share this post


Link to post
6 minutes ago, Squid said:

you can try changing the docker exec entry to be

 


docker exec $contName du -shx / 2>/dev/null

That suppresses the error, but leaves the blank size. It would be better to substitute the null size output for text like this.

activ-lazylibrarian Size: N/A, Container couldn't be started, Logs: 1.3kB

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.