rrdtool fetch output - fetch

I want to get an average of last 7 days readings from rrd database. I am using the rrdtool fetch for the same then calculating the average. I ran the following commands:
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -1days -e now | wc -l
291
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -2days -e now | wc -l
579
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -3days -e now | wc -l
126
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -4days -e now | wc -l
167
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -5days -e now | wc -l
208
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -6days -e now | wc -l
249
> rrdtool fetch /var/www/cloudrrd1/Divya.rrd AVERAGE -r 300 -s -7days -e now | wc -l
291
I am confused on the number of readings. Shouldn't it always increase with the increase in no. of days ? or am i doing something wrong ?

rrdtool fetch favores a match in coverage over a match in resolution. so if only a lower resolution rra will cover the requested time span, rrdtool will give you this and thus return fewer results ... check the timestamps in the first column of the answer.

Related

See HLS viewer number in Nginx + RTMP module

I am trying to fetch the actual number of HLS viewers in an Nginx with the RTMP module.
I have tried this solution but it is not giving me the real number, I think maybe because it's not counting the viewers who are pulling the stream in HLS format.
Is there any 'good' way to achieve that?
Thanks.
There is a way by counting the number of IPs that requested HLS/DASH fragments in the last 10 to 20 minutes:
#!/bin/bash
date_prefix=$(date +'%d/%b/%Y:%H')
cur_min=$(date +'%M')
cur_min_decimal=${cur_min:0:1}
if [[ $cur_min_decimal == '0' ]]
then
prev_min_decimal=5
else
prev_min_decimal=$(($cur_min_decimal - 1))
fi
cur_minuted_date="${date_prefix}:${cur_min_decimal}"
prev_minuted_date="${date_prefix}:${prev_min_decimal}"
tail -n 10000 /var/log/nginx/access.log \
| grep -E 'GET /(hls|dash)/key-' \
| grep -E "${cur_minuted_date}|${prev_minuted_date}" \
| awk '{print $1}' \
| sort \
| uniq \
| wc -l
If you want to show it on the website, you can CRON that script every minute and output it in the /var/www/html folder

UNIX for loop with some options

I have for loop:
for mnt `cat $file.txt`
do
grep -h -i -A 3 -B 4 *log | grep -v "10001" >> extrafile.txt
done
What does -A 3 and -B 4 means?
After and Before followed by number of lines
After en Before. After and before what?
No wonder the grep is confusing: You don't mention the "${mnt}" you are searching for. When I improve your script (moving input and output to the end, outside the loop, and using ${mnt}), the script looks like
while read -r mnt; do
grep -h -i -A 3 -B 4 "${mnt}" *log | grep -v "10001"
done < "${file.txt}" >> extrafile.txt
You get the context of every hit from $file.txt and delete all lines with 10001.

UNIX grep command (grep -v grep)

I was going through something and found this which I could not understand,
grep -v grep
What does this signify? I know that -v switch will select all the lines that do not match. But why the second grep?
This is the full command:
ps -ef | grep rsync -avz \
| grep oradata${DAY}_[0-1][0-9] \
| grep -v grep \
| awk '{print $2}' | wc -l
grep when used with ps -ef also outputs the grep used for filtering the output of ps -ef.
grep -v grep means that do not include the grep used for filtering in the command output.
You can also avoid grep in the results by using a regex pattern.
For example, in the following example you won't need a grep -v grep to avoid grep in the output:
ps -ef | grep [r]sync
Here's another example, showing different commands and their output, notice the first one where grep is also in the output whereas in the last two grep is not printed in the output:
$ ps -ef | grep ipython
501 18055 18031 0 12:44AM ttys000 0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
501 18056 18055 0 12:44AM ttys000 0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean
501 18067 18031 0 12:44AM ttys000 0:00.00 grep ipython
$ ps -ef | grep ipython | grep -v grep
501 18055 18031 0 12:44AM ttys000 0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
501 18056 18055 0 12:44AM ttys000 0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean
$ ps -ef | grep [i]python
501 18055 18031 0 12:44AM ttys000 0:00.00 /bin/bash /Users/amit/anaconda/bin/python.app /Users/amit/anaconda/bin/ipython notebook --profile=ocean
501 18056 18055 0 12:44AM ttys000 0:00.85 /Users/amit/anaconda/python.app/Contents/MacOS/python /Users/amit/anaconda/bin/ipython notebook --profile=ocean

fetch the value of number of active threads in a process

I am trying to fetch the number of threads of a process in a UNIX using command line. After going through the man page of unix command, I learnt that following command:
ps -o nlwp <pid>
returns the number of threads spawned in a process.
Whenever i executed above command in unix, it returned:
NLWP
7
Now, I want to neglect NLWP and a space before 7.
That is I am just interested in a value, as I will be using it in a script, that I am writing for unit testing?
Is it possible to fetch only value, and neglect everything(Title NLWP, space)?
You can always use the --no-headers option in ps to get rid of the headers.
In that case, use awk to just print the first value:
ps --no-headers -o nlwp <pid> | awk '{print $1}'
Or tr to remove the spaces:
ps --no-headers -o nlwp <pid> | tr -d ' '
If --no-headers is not supported in your ps version, either of these make it:
ps -o nlwp <pid> | awk 'END {print $1}'
ps -o nlwp <pid> | tail -1 | tr -d' '

How to get PID of process by specifying process name and store it in a variable to use further?

By using "ucbps" command i am able to get all PIDs
$ ucbps
Userid PID CPU % Mem % FD Used Server Port
=========================================================================
512 5783 2.50 16.30 350 managed1_adrrtwls02 61001
512 8896 2.70 21.10 393 admin_adrrtwls02 61000
512 9053 2.70 17.10 351 managed2_adrrtwls02 61002
I want to do it like this, but don't know how to do
variable=get pid of process by processname.
Then use this command kill -9 variable.
If you want to kill -9 based on a string (you might want to try kill first) you can do something like this:
ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}'
This will show you what you're about to kill (very, very important) and just pipe it to sh when the time comes to execute:
ps axf | grep <process name> | grep -v grep | awk '{print "kill -9 " $1}' | sh
pids=$(pgrep <name>)
will get you the pids of all processes with the given name. To kill them all, use
kill -9 $pids
To refrain from using a variable and directly kill all processes with a given name issue
pkill -9 <name>
On a single line...
pgrep -f process_name | xargs kill -9
Another possibility would be to use pidof it usually comes with most distributions. It will return you the PID of a given process by using it's name.
pidof process_name
This way you could store that information in a variable and execute kill -9 on it.
#!/bin/bash
pid=`pidof process_name`
kill -9 $pid
use grep [n]ame to remove that grep -v name this is first... Sec using xargs in the way how it is up there is wrong to rnu whatever it is piped you have to use -i ( interactive mode) otherwise you may have issues with the command.
ps axf | grep | grep -v grep | awk '{print "kill -9 " $1}' ?
ps aux |grep [n]ame | awk '{print "kill -9 " $2}' ? isnt that better ?

Resources