Perf trace shows not all tracepoints for every run - linux-kernel

I am trying to see tracepoints to track a ping packet's journey.
First run
vagrant#ubuntu-xenial:~$ sudo perf trace --no-syscalls --event 'net:*' ping 10.0.2.2 -c 1 |column
0.705 net:net_dev_queue:dev=enp0s3 skbaddr=0xffff88003c70d200 len=98)
0.719 net:net_dev_start_xmit:dev=enp0s3 queue_mapping=0 skbaddr=0xffff88003c70d200 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 ip_summed=0 len=98 data_len=0 network_offset=14 transport_offset_valid=1 transport_offset=34 tx_flags=0 gso_size=0 gso_segs=0 gso_type=0)
0.788 net:net_dev_xmit:dev=enp0s3 skbaddr=0xffff88003c70d200 len=98 rc=0)
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data. 1 packets transmitted, 1 received, 0% packet loss, time 0ms
64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.289 ms rtt min/avg/max/mdev = 0.289/0.289/0.289/0.000 ms
--- 10.0.2.2 ping statistics ---
On other runs of the same command it sometimes show more tracepoints. I'd expect all runs to show the same tracepoints, but it's inconsistent whether it show more of less.
vagrant#ubuntu-xenial:~$ sudo perf trace --no-syscalls --event 'net:*' ping 10.0.2.2 -c 1
PING 10.0.2.2 (10.0.2.2) 56(84) bytes of data.
64 bytes from 10.0.2.2: icmp_seq=1 ttl=64 time=0.127 ms
1.588 net:net_dev_queue:dev=enp0s3 skbaddr=0xffff88003ba9a100 len=98)
1.604 net:net_dev_start_xmit:dev=enp0s3 queue_mapping=0 skbaddr=0xffff88003ba9a100 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 ip_summed=0 len
=98 data_len=0 network_offset=14 transport_offset_valid=1 transport_offset=34 tx_flags=0 gso_size=0 gso_segs=0 gso_type=0)
1.676 net:net_dev_xmit:dev=enp0s3 skbaddr=0xffff88003ba9a100 len=98 rc=0)
1.687 net:napi_gro_receive_entry:dev=enp0s3 napi_id=0 queue_mapping=0 skbaddr=0xffff88003ba9a100 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 i
p_summed=0 hash=0x00000000 l4_hash=0 len=84 data_len=0 truesize=768 mac_header_valid=1 mac_header=-14 nr_frags=0 gso_size=0 gso_type=0)
1.695 net:netif_receive_skb:dev=enp0s3 skbaddr=0xffff88003ba9a100 len=84)
6.495 net:napi_gro_receive_entry:dev=enp0s3 napi_id=0 queue_mapping=0 skbaddr=0xffff88003ba9a600 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 i
p_summed=0 hash=0x00000000 l4_hash=0 len=46 data_len=0 truesize=704 mac_header_valid=1 mac_header=-14 nr_frags=0 gso_size=0 gso_type=0)
6.505 net:netif_receive_skb:--- 10.0.2.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.127/0.127/0.127/0.000 ms
dev=enp0s3 skbaddr=0xffff88003ba9a600 len=46)
7.618 net:napi_gro_receive_entry:dev=enp0s3 napi_id=0 queue_mapping=0 skbaddr=0xffff88003ba9a600 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 i
p_summed=0 hash=0x00000000 l4_hash=0 len=46 data_len=0 truesize=704 mac_header_valid=1 mac_header=-14 nr_frags=0 gso_size=0 gso_type=0)
7.625 net:netif_receive_skb:dev=enp0s3 skbaddr=0xffff88003ba9a600 len=46)
8.357 net:napi_gro_receive_entry:dev=enp0s3 napi_id=0 queue_mapping=0 skbaddr=0xffff88003ba9a400 vlan_tagged=0 vlan_proto=0x0000 vlan_tci=0x0000 protocol=0x0800 i
p_summed=0 hash=0x00000000 l4_hash=0 len=46 data_len=0 truesize=704 mac_header_valid=1 mac_header=-14 nr_frags=0 gso_size=0 gso_type=0)
8.364 net:netif_receive_skb:dev=enp0s3 skbaddr=0xffff88003ba9a400 len=46)
I am able to run this any number of time and output will is not predictable.

Related

Need help figuring out why this shell code is not working [duplicate]

This question already has answers here:
How to compare strings in Bash
(12 answers)
Closed 2 years ago.
Ok I need help figuring out why this code doesn't work, I've listed my problem below the code.
#!/bin/bash
if [ "$1" == "" ]
then
echo "You forgot an IP adress!"
echo "Syntax: ./ipsweep.sh xxx.xxx.x"
else
for ip in `seq 1 254`; do
ping -c 1 $1.$ip | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done
fi
now when I run the command ./ipsweeper.sh the program still runs even though the input is nothing. Please help can't see where it fails.
This:
if [ "$1" == "" ]
should be changed to:
if [ -z "$1" ]
-z is true if the string is zero length.
== is used with [[ ]] while = is used with [ ].
You can read more about bash string comparison in How to Compare Strings in Bash.
You can also check for...
pingit(){
ping -c1 ${1}
}
if [ ${#} -gt 0 ]
then
pingit ${1}
fi
...the number of arguments. Then you can source it without argument or use it with argument...
# . pingit.sh
# pingit localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.058 ms
--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.058/0.058/0.058/0.000 ms
# sh pingit.sh localhost
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.031 ms
--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.031/0.031/0.031/0.000 ms

How to use startmsg.regex in Rsyslog

The following is my conf file. I want to add config for startmsg.regex.
I added the following line in my config file
startmsg.regex="^[[:digit:]]{4}\/[[:digit:]]{2}\/[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}"
ModLoad imfile
$InputFilePollInterval 10
$PrivDropToGroup proxy
$WorkDirectory /var/spool/rsyslog
$InputFileName /var/log/app/cache.log
$InputFileTag app-error:
$InputFileStateFile stat-app-error
$InputFileSeverity error
$InputFilePersistStateInterval 20000
$InputRunFileMonitor
startmsg.regex="^[[:digit:]]{4}\/[[:digit:]]{2}\/[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}"
$template AppError,"error %msg%\n"
if $programname == 'app-error' then ##0.0.0.0:12345;AppError
if $programname == 'app-error' then ~
And when I check my config file using rsyslogd -N1, I am getting error.
What is the correct way to use this feature ?
Following is my conf file which I am using to send my logs via rsyslog.
I am also adding sample logs.
This configuration will match that each event starts with YYYY/MM/DD HH:MM:SS
and then send to my TCP endpoint.
This configuration can be used with multi line logs
module(load="imfile" PollingInterval="10") #needs to be done just once
# File 1
input(type="imfile"
File="/var/log/app/my.log"
Tag="app-error"
Severity="error"
startmsg.regex="^[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2} [[:digit:]]{1,2}:[[:digit:]]{1,2}:[[:digit:]]{1,2}"
)
$PrivDropToGroup proxy
$WorkDirectory /var/spool/rsyslog
$template AppError,"error %msg%\n"
if $programname == 'app-error' then ##0.0.0.0:12345;AppError
if $programname == 'app-error' then ~
Sample Log :
2017/10/24 09:14:06 id1| Took 0.00 seconds ( 0.00 entries/sec).
CPU Usage: 0.052 seconds = 0.032 user + 0.020 sys
Maximum Resident Size: 104944 KB
Page faults with physical i/o: 0
2017/10/24 09:14:06 id1| found error
Now rsyslog will send my multi line logs as a single event to my tcp end point as follows :
2017/10/24 09:14:06 id1| Took 0.00 seconds ( 0.00 entries/sec). \nCPU Usage: 0.052 seconds = 0.032 user + 0.020 sys \nMaximum Resident Size: 104944 KB \nPage faults with physical i/o: 0
2017/10/24 09:14:06 id1| found error

How to issue multiple commands to stdin in linux shell with <<<printf

I'm exercising with DVWA high level command injection. I know there is a hole for |, but I'm looking for a way to get an output like the following:
root#vwksOffensive:~# ping -c 4 10.0.0.1 ; ls
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3066ms
a a_post Documenti Immagini Modelli Musica pocl rockyou.txt Scrivania WebScarab.properties
Add b hash JavaSnoop.properties mtu plain Pubblici Scaricati Video
root#vwksOffensive:~#
starting from
<<<printf "[ping ip argument] \u003B the_command_I_choose"
My problem is that can do this:
root#vwksOffensive:~# ping -c 4 <<<printf "10.0.0.1"
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
--- 10.0.0.1 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3055ms
root#vwksOffensive:~#
but I'm not able to do this:
root#vwksOffensive:~# ping -c 4 <<<printf "10.0.0.1 \u003b ls"
PING 10.0.0.1 \u003b ls (10.0.0.1) 56(84) bytes of data.
--- 10.0.0.1 \u003b ls ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3075ms
root#vwksOffensive:~#
and not even this:
root#vwksOffensive:~# ping -c 4 <<<printf "10.0.0.1 ; ls"
PING 10.0.0.1 ; ls (10.0.0.1) 56(84) bytes of data.
--- 10.0.0.1 ; ls ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3075ms
root#vwksOffensive:~#
I searched deeply and the most similar, if it could be said so, is the first answer to this question:
How to make a bash function which can read from standard input?
Unfortunately, this did not help me completely, so I decided to post here my question because I'm sure that, in my ignorance, I'm missing something.
The solution must contain the char ; or any other needed special char coded in unicode. The forbidden, substituted with blank, char are the following:
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
if the "command you choose" is in a variable like this:
mycmd="ls -l"
then you can wrap your command and other commands in a sub-shell surrounded by this: ( )
The output for the sub-shell can be re-directed into any other command that you want. e.g. tail, grep, > $log, etc.
The ip addresses that you want to send ping can also be in a variable name:
myip=10.0.0.1
(ping $myip ; $mycmd ) > logfile.txt
It is not clear why you would want to send it in with <<< instead of directly on the command line. If you must use <<<, then still try wrapping the commands in () parenthesis and put a carriage return after the <<< 10.0.0..
and the "ls -l". The parenthesis will tell bash that the command is not done until the closing parenthesis is seen. The carriage return will stop the <<< from consuming the next command.

Error when executing ping command

host.txt:
www.google.com
test.sh:
#!/usr/bin/env bash
while IFS=$'\n' read -r line; do
echo $line
echo "#1"
ping -c 1 $line
line2="www.google.com"
echo "#2"
ping -c 1 $line2
done < $hostfile
exit 0
output:
> test.sh
www.google.com
#1
ping: unknown host www.google.com
#2
PING www.google.com (74.125.206.147) 56(84) bytes of data.
64 bytes from wk-in-f147.1e100.net (74.125.206.147): icmp_seq=1 ttl=46 time=22.1 ms
--- www.google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 22.111/22.111/22.111/0.000 ms
Could someone tell why first ping failed?
Thanks.
Possibly your txt file contains CRLF line endings
and bash actually tried to ping www.google.com^M name.

Retry a Bash command with timeout

How to retry a bash command until its status is ok or until a timeout is reached?
My best shot (I'm looking for something simpler):
NEXT_WAIT_TIME=0
COMMAND_STATUS=1
until [ $COMMAND_STATUS -eq 0 || $NEXT_WAIT_TIME -eq 4 ]; do
command
COMMAND_STATUS=$?
sleep $NEXT_WAIT_TIME
let NEXT_WAIT_TIME=NEXT_WAIT_TIME+1
done
You can simplify things a bit by putting command right in the test and doing increments a bit differently. Otherwise the script looks fine:
NEXT_WAIT_TIME=0
until [ $NEXT_WAIT_TIME -eq 5 ] || command; do
sleep $(( NEXT_WAIT_TIME++ ))
done
[ $NEXT_WAIT_TIME -lt 5 ]
One line and shortest, and maybe the best approach:
timeout 12h bash -c 'until ssh root#mynewvm; do sleep 10; done'
Credited by http://jeromebelleman.gitlab.io/posts/devops/until/
retry fuction is from:
http://fahdshariff.blogspot.com/2014/02/retrying-commands-in-shell-scripts.html
#!/bin/bash
# Retries a command on failure.
# $1 - the max number of attempts
# $2... - the command to run
retry() {
local -r -i max_attempts="$1"; shift
local -r cmd="$#"
local -i attempt_num=1
until $cmd
do
if (( attempt_num == max_attempts ))
then
echo "Attempt $attempt_num failed and there are no more attempts left!"
return 1
else
echo "Attempt $attempt_num failed! Trying again in $attempt_num seconds..."
sleep $(( attempt_num++ ))
fi
done
}
# example usage:
retry 5 ls -ltr foo
if you want to retry an function in your script, you should do like this:
# example usage:
foo()
{
#whatever you want do.
}
declare -fxr foo
retry 3 timeout 60 bash -ce 'foo'
Put together some tools.
retry: https://github.com/kadwanev/retry
timeout: http://manpages.courier-mta.org/htmlman1/timeout.1.html
Then see the magic
retry timeout 3 ping google.com
PING google.com (173.194.123.97): 56 data bytes
64 bytes from 173.194.123.97: icmp_seq=0 ttl=55 time=13.982 ms
64 bytes from 173.194.123.97: icmp_seq=1 ttl=55 time=44.857 ms
64 bytes from 173.194.123.97: icmp_seq=2 ttl=55 time=64.187 ms
Before retry #1: sleeping 0.3 seconds
PING google.com (173.194.123.103): 56 data bytes
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=56.549 ms
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=60.220 ms
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=8.872 ms
Before retry #2: sleeping 0.6 seconds
PING google.com (173.194.123.103): 56 data bytes
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=25.819 ms
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=16.382 ms
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=3.224 ms
Before retry #3: sleeping 1.2 seconds
PING google.com (173.194.123.103): 56 data bytes
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=58.438 ms
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=94.828 ms
64 bytes from 173.194.123.103: icmp_seq=2 ttl=55 time=61.075 ms
Before retry #4: sleeping 2.4 seconds
PING google.com (173.194.123.103): 56 data bytes
64 bytes from 173.194.123.103: icmp_seq=0 ttl=55 time=43.361 ms
64 bytes from 173.194.123.103: icmp_seq=1 ttl=55 time=32.171 ms
...
Check exit status for ultimate pass/fail.
For anyone wanting to actually wait until some time passed, taking into account the time of your command might be significant:
TIMEOUT_SEC=180
start_time="$(date -u +%s)"
while [ condition_or_just_true ]; do
current_time="$(date -u +%s)"
elapsed_seconds=$(($current_time-$start_time))
if [ $elapsed_seconds -gt $TIMEOUT_SEC ]; then
echo "timeout of $TIMEOUT_SEC sec"
exit 1
fi
echo "another attempt (elapsed $elapsed_seconds sec)"
some_command_and_maybe_sleep
done
I made some tweaks to this answer which let you switch on whether the timeout was reached, or whether the command succeed. Also, in this version there is a retry every second:
ELAPSED=0
started=$(mktemp)
echo "False" > $started
until the_command_here && echo "True" > $started || [ $ELAPSED -eq 30 ]
do
sleep 1
(( ELAPSED++ ))
done
if [[ $(cat $started) == "True" ]]
then
echo "the command completed after $ELAPSED seconds"
else
echo "timed out after $ELAPSED seconds"
exit 111
fi

Resources