-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevopsfetch
executable file
·157 lines (139 loc) · 5.18 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash
# Define functions for each information required
# Check if the script is running as root
if [ "$(id -u)" != "0" ]; then
echo "This script requires root privileges. Please run with sudo or run as root"
exit 1
fi
# Function to display active ports and services
ports() {
if [ -z "$1" ]; then
echo "Active Ports and Services:"
echo "Port Protocol Service PID Command"
# Use lsof to list listening ports, format output, and display as a table
sudo lsof -i -P -n | grep LISTEN | awk '{print $9, $1, $2}' | sed 's/://' | sort -n | while read port prog pid; do
proto=$(echo $port | cut -d'(' -f2 | cut -d')' -f1)
port=$(echo $port | cut -d'(' -f1)
cmd=$(ps -p $pid -o comm= | head -1)
echo "$port $proto $prog $pid $cmd"
done | column -t
else
# Show specific details for a port
echo "Details for port $1:"
sudo lsof -i :$1 -P -n | column -t
fi
}
# Function to display Docker images and containers
docker_info() {
if [ -z "$1" ]; then
echo "Docker Images:"
docker images | column -t
echo
echo "Docker Containers:"
docker ps -a | column -t
else
# Show specific details for a container
echo "Details for container $1:"
docker inspect "$1" | jq '.[0] | {Name: .Name, State: .State, Mounts: .Mounts, NetworkSettings: .NetworkSettings}'
fi
}
# Function to display Nginx configurations
nginx() {
if [ -z "$1" ]; then
echo "Nginx Configurations:"
echo "Config File | Domain | Proxy"
# Extract server_name and proxy_pass information from Nginx config files
grep -H -r "server_name\|proxy_pass" /etc/nginx/sites-enabled/* | awk '
/server_name/ { split($0, a, ":"); file=a[1]; domain=$3; gsub(/;/, "", domain) }
/proxy_pass/ { proxy=$3; gsub(/;/, "", proxy); print file " | " domain " | " proxy }
' | column -t -s '|'
else
# Show details for a specific domain
domain="$1"
echo "Details for domain $domain:"
config_file=$(grep -l "server_name.*$domain" /etc/nginx/sites-enabled/*)
if [ -n "$config_file" ]; then
echo "Configuration file: $config_file"
echo "Server block:"
sed -n '/server {/,/}/p' "$config_file" | sed 's/^/ /'
echo "Listening ports:"
grep "listen" "$config_file" | sed 's/^/ /'
echo "Proxy configuration:"
grep "proxy_pass" "$config_file" | sed 's/^/ /'
echo "SSL configuration:"
grep "ssl_certificate" "$config_file" | sed 's/^/ /'
else
echo "Domain '$domain' not found in Nginx configurations."
fi
fi
}
# Function to display users and last login times
users() {
if [ -z "$1" ]; then
echo "Users and Last Login Times:"
seen=()
echo "User last login"
# Filter out reboot and wtmp entries, and print unique users
last | awk '/^[^w]/ {if (!seen[$1]) {print $1, $3, $4, $5; seen[$1] = 1}}' | column -t
else
# Show specific details for a user
echo "Details for user $1:"
id "$1"
echo "Last login:"
last "$1" | head -n 1
fi
}
# Function to display activities within a specified time range
time_range() {
if [ -z "$1" ]; then
echo "Error: No date provided. Usage: devopsfetch -t YYYY-MM-DD [YYYY-MM-DD]"
return 1
fi
start_date=$1
end_date=${2:-$start_date}
echo "Activities from $start_date to $end_date:"
journalctl --since "$start_date" --until "$end_date 23:59:59" | tail -n 50
}
# Help
help(){
echo """Usage: devopsfetch [OPTIONS]
Your devops tool for server information retrieval and monitoring
Global Options:
-p --port Display all active ports and services
Use -p <port_number> to get detailed information about a specific port
-d --docker List all Docker images and containers
Use -d <container_name> to get detailed information about a specific container
-n --nginx Display all Nginx domains and their ports
Use -n <domain> to get detailed configuration information for a specific domain
-u --users List all users and their last login times
Use -u <username> ls
to get detailed information about a specific user
-t --time Display activities within a specified time range
-h --help Provide usage instructions for the program"""
}
# Process options when the devopsfetch is called
case "$1" in
-p|--ports)
ports "$2"
;;
-d|--docker)
docker_info "$2"
;;
-n|--nginx)
nginx "$2"
;;
-u|--users)
users "$2"
;;
-t|--time)
time_range "$2" "$3"
;;
-h|--help)
help
;;
*)
echo "Invalid option. Use -h or --help for usage information."
help
exit 1
;;
esac