-
Notifications
You must be signed in to change notification settings - Fork 0
/
devopsfetch
103 lines (94 loc) · 3.1 KB
/
devopsfetch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
# Function to display ports
display_ports() {
echo "USER PORT SERVICE"
if [ -z "$1" ]; then
ss -tuln | awk 'NR>1 {split($5, a, ":"); print a[length(a)]}' | sort -u | while read port; do
lsof -i :$port -sTCP:LISTEN -n -P 2>/dev/null | awk 'NR>1 {print $3, $9, $1}' | awk -v p=$port '{split($2, a, ":"); if (a[length(a)] == p) print $1, p, $3}' | sort -u
done
else
lsof -i :$1 -sTCP:LISTEN -n -P 2>/dev/null | awk 'NR>1 {print $3, $9, $1}' | awk -v p=$1 '{split($2, a, ":"); if (a[length(a)] == p) print $1, p, $3}' | sort -u
fi | column -t
}
# Function to display Docker info
display_docker() {
if [ -z "$1" ]; then
docker ps -a --format "table {{.Names}}\t{{.Status}}\t{{.Image}}\t{{.ID}}"
else
docker inspect "$1" | jq '.[0] | {Name: .Name, State: .State.Status, Image: .Config.Image, ID: .Id}'
fi
}
# Function to display Nginx info
display_nginx() {
if [ -z "$1" ]; then
grep -R server_name /etc/nginx/sites-enabled/ | awk '{print $2 " " $3}' | sed 's/;//' | column -t
else
grep -R -A 10 "server_name $1" /etc/nginx/sites-enabled/
fi
}
# Function to display user info
display_users() {
if [ -z "$1" ]; then
last | head -n 10 | column -t
else
last "$1" | head -n 5 | column -t
fi
}
display_time_range() {
if [ $# -eq 0 ]; then
echo "Please specify a time range or date"
return 1
elif [ $# -eq 1 ]; then
# Single date provided
start_date=$(date -d "$1" +"%Y-%m-%d 00:00:00")
end_date=$(date -d "$1 + 1 day" +"%Y-%m-%d 00:00:00")
elif [ $# -eq 2 ]; then
# Date range provided
start_date=$(date -d "$1" +"%Y-%m-%d 00:00:00")
end_date=$(date -d "$2 + 1 day" +"%Y-%m-%d 00:00:00")
else
echo "Invalid number of arguments for time range"
return 1
fi
# Use journalctl to display logs within the specified time range
journalctl --since "$start_date" --until "$end_date"
}
# Function to display help
display_help() {
echo "Usage: devopsfetch [OPTION]"
echo "Retrieve and display system information for DevOps purposes."
echo
echo "Options:"
echo " -p, --port [PORT] Display active ports or specific port info"
echo " -d, --docker [NAME] List Docker images/containers or specific container info"
echo " -n, --nginx [DOMAIN] Display Nginx domains or specific domain config"
echo " -u, --users [USER] List users and last login or specific user info"
echo " -t, --time RANGE Display activities within a time range (e.g., '1 hour ago')"
echo " -h, --help Display this help message"
}
# Main logic
case "$1" in
-p|--port)
display_ports "$2"
;;
-d|--docker)
display_docker "$2"
;;
-n|--nginx)
display_nginx "$2"
;;
-u|--users)
display_users "$2"
;;
-t|--time)
shift
display_time_range "$@"
;;
-h|--help)
display_help
;;
*)
echo "Invalid option. Use -h or --help for usage information."
exit 1
;;
esac