-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_info.command
executable file
·208 lines (154 loc) · 7.1 KB
/
system_info.command
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env bash
SCRIPT_NAME="system_info.command"
JSON_OUTPUT_FILE="system_info.json"
# Check if .env is found, exit if not
if [ ! -f .env ]; then
echo "❌ Error: .env file not found."
exit 1
fi
# Import env variables from .env
set -a
source .env
# Check if logger is found, exit if not
if [ ! -f utils/logger.sh ]; then
echo "❌ Error: utils/logger.sh not found. | $SCRIPT_NAME"
exit 1
fi
# Import logger
source utils/logger.sh
# Check if spinner is found, exit if not
if [ ! -f utils/spinner.sh ]; then
log_error "utils/spinner.sh not found. Exiting."
echo "❌ Error: utils/spinner.sh not found."
exit 1
fi
# Import spinner
source utils/spinner.sh
# import geolocation
if [ ! -f utils/geolocation.command ]; then
log_error "utils/geolocation.command not found. Exiting."
echo "❌ Error: utils/geolocation.command not found."
exit 1
fi
source utils/geolocation.command
log_start "Starting the script for $SCRIPT_NAME"
# Function to gather system information
gather_system_info() {
log_debug "Gathering system information..."
local user=$(whoami)
# detailed user information (running on MacOs)
if command -v id &> /dev/null; then
# save the user information compatible with JSON format, provide at least 5 properties
local user_info=$(id -Gn $user | awk '{print "{ \"groups\": \"" $0 "\", \"uid\": \"" $1 "\", \"gid\": \"" $2 "\", \"home\": \"" $3 "\", \"shell\": \"" $4 "\" }"}')
else
local user_info="User information not available"
fi
local geolocation_info=$(print_geolocation_info)
local hostname=$(hostname)
# important paths in the system
local paths=$(echo "{ \"home\": \"$HOME\", \"temp\": \"$TMPDIR\", \"log\": \"$LOG_DIR\" }")
local timezone=$(date +'%Z %z')
local os=$(uname -s)
local kernel=$(uname -r)
local architecture=$(uname -m)
local uptime=$(uptime | awk -F, '{print $1}' | awk '{print $3,$4}')
local shell=$(echo $SHELL)
local cpu=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo "CPU information not available")
local total_mem=$(sysctl -n hw.memsize 2>/dev/null | awk '{print $1/1024/1024 " MB"}' || echo "Memory information not available")
local current_time=$(date)
# Disk usage information
local disk_usage=$(df -h | grep "^/" | awk '{print "{ \"mount\": \"" $6 "\", \"usage\": \"" $5 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# Installed packages
if command -v dpkg &> /dev/null; then
local installed_packages=$(dpkg -l | awk '{print "\"" $2 "\""}' | grep -v "^\"lib" | tr '\n' ',' | sed 's/,$//')
elif command -v brew &> /dev/null; then
local installed_packages=$(brew list | awk '{print "\"" $1 "\""}' | tr '\n' ',' | sed 's/,$//')
else
local installed_packages="\"Package information not available\""
fi
# Installed packages count
local package_count=$(echo $installed_packages | tr -cd ',' | wc -c)
package_count=$((package_count + 1))
# Installed software versions
local bash_version=$(bash --version | head -n 1 | awk '{print $4}')
local git_version=$(git --version | awk '{print $3}')
local docker_version=$(docker --version 2>/dev/null | awk '{print $3}')
local python_version=$(python3 --version 2>/dev/null | awk '{print $2}')
local node_version=$(node --version 2>/dev/null || echo "Node not installed")
local npm_version=$(npm --version 2>/dev/null || echo "NPM not installed")
# Network information
local network_info=$(ifconfig | awk '/^[a-z]/ { iface=$1 } $1 == "inet" { print "{ \"interface\": \"" iface "\", \"ip\": \"" $2 "\" }" }' | tr '\n' ',' | sed 's/,$//')
local external_ip=$(curl -s https://api.ipify.org)
# Remaining disk space
local remaining_disk_space=$(df -h / | awk 'NR==2 {print $4}')
# Potentially running processes in port 3000
local processes_in_port_3000=$(lsof -i :3000 | awk 'NR>1 {print "{ \"user\": \"" $3 "\", \"pid\": \"" $2 "\", \"command\": \"" $1 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# check if internet is available and assign this to a variable
local internet_status=$(curl -s -I www.google.com | head -n 1 | grep "HTTP/1.1" | awk '{print $2}')
# check if postgres is running, assign status to a variable in quotes
local postgres_status=$(pg_isready | awk '{print "{ \"status\": \"" $3 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# other potential information to gather
# check if the system is running on a virtual machine
local virtual_machine=$(system_profiler SPHardwareDataType | grep "Model Identifier" | awk '{print $3}' | grep -i "virtual" || echo "Not a virtual machine")
# show a summary of the gathered information
local json_content='{
"user": "'$user'",
"user_info": ['"$user_info"'],
"geolocation_info": ['"$geolocation_info"'],
"hostname": "'$hostname'",
"paths": ['"$paths"'],
"timezone": "'$timezone'",
"os": "'$os'",
"kernel": "'$kernel'",
"architecture": "'$architecture'",
"uptime": "'$uptime'",
"shell": "'$shell'",
"cpu": "'$cpu'",
"total_mem": "'$total_mem'",
"current_time": "'$current_time'",
"disk_usage": ['"$disk_usage"'],
"package_count": "'$package_count'",
"bash_version": "'$bash_version'",
"git_version": "'$git_version'",
"docker_version": "'$docker_version'",
"python_version": "'$python_version'",
"node_version": "'$node_version'",
"npm_version": "'$npm_version'",
"network_info": ['"$network_info"'],
"external_ip": "'$external_ip'",
"remaining_disk_space": "'$remaining_disk_space'",
"processes_in_port_3000": ['"$processes_in_port_3000"'],
"internet_status": "'$internet_status'",
"postgres_status": ['"$postgres_status"'],
"virtual_machine": "'$virtual_machine'"
}
'
echo "$json_content" > "$JSON_OUTPUT_FILE"
log_info "System information saved to $JSON_OUTPUT_FILE"
}
# Start gathering system information in the background
(gather_system_info) &
# Get the PID of the gather_system_info process
pid=$!
# Show spinner while gathering system information
spinner $pid
# Display a user-friendly message
echo "✅ System information has been gathered and saved to $JSON_OUTPUT_FILE"
log_end "Ending the script for $SCRIPT_NAME"
# check for potential system updates on Mac Os
#if command -v softwareupdate &> /dev/null; then
# grep the last line of the output to get the system update information
#local system_updates=$(softwareupdate -l | tail -n 1)
#else
#local system_updates="System update information not available"
#fi
# Running processes
#local running_processes=$(ps aux --sort=-%mem | awk 'NR<=10 {print "{ \"user\": \"" $1 "\", \"pid\": \"" $2 "\", \"cpu\": \"" $3 "\", \"mem\": \"" $4 "\", \"command\": \"" $11 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# Open ports
#local open_ports=$(ss -tuln | awk 'NR>1 {print "{ \"proto\": \"" $1 "\", \"local_address\": \"" $4 "\", \"foreign_address\": \"" $5 "\", \"state\": \"" $1 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# Environment variables
#local env_vars=$(env | awk -F= '{print "{ \"" $1 "\": \"" $2 "\" }"}' | tr '\n' ',' | sed 's/,$//')
# various API systems for geolocation
#local location=$(curl -s https://ipapi.co/json/)
#local location=$(curl -s https://ipinfo.io/)
#local location=$(curl -s https://freegeoip.app/json/)