Last active
July 30, 2019 21:50
-
-
Save YoussefLagtab/e254c65c4ea349c18b675ac69a94f0e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#************************************************************************************** | |
# Functions | |
usage() { | |
cat << STOP | |
Name: $0 | |
Description: User Infos | |
Usage: $0 uids... | |
columns: | |
-User : user name | |
-UID : user id | |
-GID : group id | |
-N.GR : nymber of groups a user belong | |
-N.PS : number of processes running owned by a user | |
-%MEM : memory percentage | |
-%CPU : cpu percentage | |
-last_login : last login | |
STOP | |
} | |
user() | |
{ | |
id -un $uid; | |
} | |
gid() { | |
id $uid -g | |
} | |
n_groups() | |
{ | |
groups `user` | cut -d: -f2 | wc -w | |
} | |
n_ps() | |
{ | |
ps aux | grep ^`user` | wc -l | |
} | |
mem() | |
{ | |
ps aux | grep ^`user` | awk 'BEGIN {mem=0.0}; {mem += $4}END {printf "%.1f\n", mem}' | |
} | |
cpu() | |
{ | |
ps aux | grep ^`user` | awk 'BEGIN {cpu=0.0}; {cpu += $4}END {printf "%.1f\n", cpu}' | |
} | |
_last() | |
{ | |
last `user` | grep -o "\(Mon\|Tue\|Wed\|Thu\|Fri\|Sat\|Sun\).*" | head -1 | |
} | |
header() | |
{ | |
format_output "User UID GID N.GR N.PS %MEM %CPU last_login"; | |
printf "_%.0s" {1..100}; | |
echo | |
} | |
format_output() | |
{ | |
echo $@ | awk '{printf "%-15s%-8s%-8s%-8s%-8s%-8s%-8s%s\n", $1, $2, $3, $4, $5, $6, $7, substr($0, index($0,$8)) }' | |
} | |
user_info() | |
{ | |
format_output "`user` $uid `gid` `n_groups` `n_ps` `mem` `cpu` `_last`"; | |
} | |
main() | |
{ | |
h=1; | |
for uid in $@ | |
do | |
id $uid 1>/dev/null 2>/dev/null; | |
if [ $? -ne 1 ] | |
then | |
if [ $h -eq 1 ] | |
then | |
header; | |
h=0; | |
fi | |
format_output `user_info`; | |
fi | |
done | |
} | |
#************************************************************************************** | |
# Program Start | |
if [ $# -lt 1 ] | |
then | |
usage; | |
exit 1; | |
fi | |
main $@; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment