Grab a string starting with a certain sub-string [duplicate] - linux

This question already has answers here:
How to grep for contents after pattern?
(8 answers)
Closed 4 years ago.
I want to grab the cpu model of an android device using
cat /proc/cpuinfo
The output is something like this
Processor : AArch64 Processor rev 3 (aarch64)
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5
Features : fp asimd evtstrm aes pmull sha1 sha2 crc32
CPU implementer : 0x41
CPU architecture: 8
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 3
Hardware : Qualcomm Technologies, Inc msm8992
Revision : 000b
Now what I need is only the "msm8992". The last 4 numbers change from devices to another, So what I was thinking is to search for word that starts with "msm" and extract it but I am struggling with grep / awk commands.

perhaps search for "Hardware" and extract the last word
$ awk '/^Hardware/{print $NF}' /proc/cpuinfo
grep -oE 'msm[0-9]+' might do as well.

since you tag awk, I used awk for It. Please check it out
grep msm /proc/cpuinfo | awk '{print substr($0, length($0)-6,7)}'

Related

Different uname -a output

I am looking for different output of the uname -a command.
The one that I have is
Linux dogeserv.net 5.4.0-89-generic #100-Ubuntu SMP Fri Sep 24 14:50:10 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
I am looking for different outputs of Processor (other then x86_64 and aarch64), OS , -m output.
As per the time i am writing the answer, these options are available in uname command.
-a, --all print all information, in the following order, except omit -p and -i if unknown:
-s, --kernel-name print the kernel name
-n, --nodename print the network node hostname
-r, --kernel-release print the kernel release
-v, --kernel-version print the kernel version
-m, --machine print the machine hardware name
-p, --processor print the processor type (non-portable)
-i, --hardware-platform print the hardware platform (non-portable)
-o, --operating-system print the operating system
For some reason, it's not showing my processor, but you can use uname -p for processor information.
Incase you want detailed information about your processor, you can use cat /proc/cpuinfo, and this will show you information about the processor of your computer with information of every single core and this is what your output should look like
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 142
model name : Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
stepping : 10
microcode : 0xea
cpu MHz : 1279.215
cache size : 4096 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 2
.....
TO be more specific you can use these commands for their respective outputs
cat /proc/cpuinfo | grep 'vendor' | uniq #view vendor name
cat /proc/cpuinfo | grep 'model name' | uniq #display model name
cat /proc/cpuinfo | grep processor | wc -l #count the number of processing units
cat /proc/cpuinfo | grep 'core id' #show individual cores
A alternative to cat /proc/cpuinfo is lscpu, it will also give you information about your processor, but it will be more about the whole processor instead of every single core.
this is what output should look like:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 39 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: GenuineIntel
BIOS Vendor ID: Intel(R) Corporation
Model name: Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
BIOS Model name: Intel(R) Core(TM) i3-8130U CPU # 2.20GHz
CPU family: 6
Model: 142
Thread(s) per core: 2
Core(s) per socket: 2
.....
Lastly stackoverflow is for mostly programming related questions, you can see all the sites Stack Exchange offers for different type of questions to be asked by clicking this Link

Can I increase linux entropy by using rng-daemon without hardware generator?

I want to continuously increase /prco/sys/random/entropy_avail when it reduced.
I first check the rngd (https://wiki.archlinux.org/index.php/Rng-tools)
It says /dev/random is very slow since it only collects entropy from device drivers and other (slow) sources and I think that is why we use rngd.
And it says rngd mainly uses hardware random number generators (TRNG), present in modern hardware like recent AMD/Intel processors, VIA Nano or even Raspberry Pi.
However, when I start rngd it says
[root#localhost init.d]# rngd
can't open entropy source(tpm or intel/amd rng)
Maybe RNG device modules are not loaded
But I don't have Intel RDRAND confirmed by cat /proc/cpuinfo | grep rdrand:
[root#localhost init.d]# cat /proc/cpuinfo | grep rdrand | wc -l
0
If there is any possible resources that I can use?
Alternatively, is it possible making script to increase /proc/sys/random/entropy_avail?
Try this:
sudo apt-get install haveged

get available memory in gb using single bash shell command

following command returns available memory in kilobytes
cat /proc/meminfo | grep MemFree | awk '{ print $2 }'
can some one suggest single command to get the available memory in gb?
Just a slight modification to your own magical incantation:
awk '/MemFree/ { printf "%.3f \n", $2/1024/1024 }' /proc/meminfo
P.S.: Dear OP, if you find yourself invoking grep & awk in one line you're most likely doing it wrong ;} ... Same with invoking cat on a single file; that's hardly ever warranted.
Most simple is the following :
free -h
Following is the output Screenshot :
More details :
DESCRIPTION
free - displays the total amount of free and used physical and swap mem‐
ory in the system, as well as the buffers and caches used by the ker‐
nel. The information is gathered by parsing /proc/meminfo. The dis‐
played columns are:
total Total installed memory (MemTotal and SwapTotal in /proc/meminfo)
used Used memory (calculated as total - free - buffers - cache)
free Unused memory (MemFree and SwapFree in /proc/meminfo)
shared Memory used (mostly) by tmpfs (Shmem in /proc/meminfo, available
on kernels 2.6.32, displayed as zero if not available)
buffers
Memory used by kernel buffers (Buffers in /proc/meminfo)
cache Memory used by the page cache and slabs (Cached and Slab in
/proc/meminfo)
buff/cache
Sum of buffers and cache
available
Estimation of how much memory is available for starting new
applications, without swapping. Unlike the data provided by the
cache or free fields, this field takes into account page cache
and also that not all reclaimable memory slabs will be reclaimed
due to items being in use (MemAvailable in /proc/meminfo, avail‐
able on kernels 3.14, emulated on kernels 2.6.27+, otherwise the
same as free)
freemem_in_gb () {
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=3;${freemem}/1024/1024"
}
Please notice that scale=3 can be changed to some other value, for a better precision.
So, for example one could write a function that will take a precision argument, like so:
freemem_in_gb () {
prec=$1;
read -r _ freemem _ <<< "$(grep --fixed-strings 'MemFree' /proc/meminfo)"
bc <<< "scale=${prec:-3};${freemem}/1024/1024"
}
Which will take (or use 3 as a default value) and pass a precision argument to bc's scale option
Usage example:
$ freemem_in_gb
5.524
$ freemem_in_gb 7
5.5115814
EDIT
Thanks for #Stephen P and #Etan Reisner for leaving a comment and improving this answer.
Code edited accordingly.
grep's long option --fixed-strings is used purposely instead of -F or fgrep for explanatory reasons.
Yet another way:
expr $(sed -n '/^MemTotal:/ s/[^[:digit:]]//gp' /proc/meminfo) / 1024 / 1024
Also a bit shorter:
expr $(sed -n '/^MemTotal:/ s/[^0-9]//gp' /proc/meminfo) / 1024 / 1024
And if you like bc and precision that much:
bc <<< "scale=2; $(sed -n '/^MemTotal:/ s/[^[:digit:]]//gp' /proc/meminfo) / 1024 / 1024 "
If you have python, you can do it this way:
To get total available memory:
python -c "import os;print(int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3)))"
In this example, I used round to round to the nearest GB. You can make it into a shell function like so:
get_mem(){
MEM=$(python -c "import os;print(int(round(os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES') / 1024.0**3)))")
echo $MEM
}
To get free and used memory check out psutil here.
Whilst I agree that dividing by 1024 should be more correct, I find on my various cloud and physical servers, this gives neater output:
free -m | awk '/^Mem:/{printf("%.1fGb\n",$2/1000)}'

How do you get the set of available CPUs in a Linux kernel module?

I would like to start one kernel thread per CPU with kthread_create()/kthread_bind(). However, I can't for the life of me figure out how to query the number of available CPUs. I did find the CPU_SET man page but that didn't help either.
Any thoughts?
You can use num_online_cpus() to get the number of available cpus. This may be different from things like nr_cpu_ids if the system was booted using a maxcpus setting that is not the same as the number of cpus in the system.
See following link, cpuinfo.c, proc.c, may be help you. And
at line 143, you can use two functions for traversing cpus, cpumask_first, cpumask_next. I think, by try and error, you can find the solutions.
You can use x86info. It's not per default installed (sudo apt-get install x86info (ubuntu))
x86info | grep Found
Found 2 CPUs
another way is:
grep processor /proc/cpuinfo | wc -l
2
Is that you are looking for?
If you're using a system that is Fedora Linux / RHEL / CentOS v6+ / Debian Linux v6+ you can use lscpu:
michael#test:~$ lscpu
Architecture: i686
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 37
Stepping: 5
CPU MHz: 1199.000
BogoMIPS: 5319.88
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
Particularly you might be interested in the -p option which gives you parseable output:
michael#test:~$ lscpu -p
# The following is the parsable format, which can be fed to other
# programs. Each different item in every column has an unique ID
# starting from zero.
# CPU,Core,Socket,Node,,L1d,L1i,L2,L3
0,0,0,,,0,0,0,0
1,0,0,,,0,0,0,0
2,1,0,,,1,1,1,0
3,1,0,,,1,1,1,0
$ nproc --all
4
--all print the number of installed processors

How to measure CPU usage

I would like to log CPU usage at a frequency of 1 second.
One possible way to do it is via vmstat 1 command.
The problem is that the time between each output is not always exactly one second, especially on a busy server. I would like to be able to output the timestamp along with the CPU usage every second. What would be a simple way to accomplish this, without installing special tools?
There are many ways to do that. Except top another way is to you the "sar" utility. So something like
sar -u 1 10
will give you the cpu utilization for 10 times every 1 second. At the end it will print averages for each one of the sys, user, iowait, idle
Another utility is the "mpstat", that gives you similar things with sar
Use the well-known UNIX tool top that is normally available on Linux systems:
top -b -d 1 > /tmp/top.log
The first line of each output block from top contains a timestamp.
I see no command line option to limit the number of rows that top displays.
Section 5a. SYSTEM Configuration File and 5b. PERSONAL Configuration File of the top man page describes pressing W when running top in interactive mode to create a $HOME/.toprc configuration file.
I did this, then edited my .toprc file and changed all maxtasks values so that they are maxtasks=4. Then top only displays 4 rows of output.
For completeness, the alternative way to do this using pipes is:
top -b -d 1 | awk '/load average/ {n=10} {if (n-- > 0) {print}}' > /tmp/top.log
You might want to try htop and atop. htop is beautifully interactive while atop gathers information and can report CPU usage even for terminated processes.
I found a neat way to get the timestamp information to be displayed along with the output of vmstat.
Sample command:
vmstat -n 1 3 | while read line; do echo "$(date --iso-8601=seconds) $line"; done
Output:
2013-09-13T14:01:31-0700 procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
2013-09-13T14:01:31-0700 r b swpd free buff cache si so bi bo in cs us sy id wa
2013-09-13T14:01:31-0700 1 1 4197640 29952 124584 12477708 12 5 449 147 2 0 7 4 82 7
2013-09-13T14:01:32-0700 3 0 4197780 28232 124504 12480324 392 180 15984 180 1792 1301 31 15 38 16
2013-09-13T14:01:33-0700 0 1 4197656 30464 124504 12477492 344 0 2008 0 1892 1929 32 14 43 10
To monitor the disk usage, cpu and load i created a small bash scripts that writes the values to a log file every 10 seconds.
This logfile is processed by logstash kibana and riemann.
# #!/usr/bin/env bash
# Define a timestamp function
LOGPATH="/var/log/systemstatus.log"
timestamp() {
date +"%Y-%m-%dT%T.%N"
}
#server load
while ( sleep 10 ) ; do
echo -n "$(timestamp) linux::systemstatus::load " >> $LOGPATH
cat /proc/loadavg >> $LOGPATH
#cpu usage
echo -n "$(timestamp) linux::systemstatus::cpu " >> $LOGPATH
top -bn 1 | sed -n 3p >> $LOGPAT
#disk usage
echo -n "$(timestamp) linux::systemstatus::storage " >> $LOGPATH
df --total|grep total|sed "s/total//g"| sed 's/^ *//' >> $LOGPATH
done

Resources