-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm2_logs.sh
159 lines (149 loc) · 3.86 KB
/
pm2_logs.sh
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
#!/bin/bash
logs='/home/web/.pm2/logs'
email_folder='~/.pm2/.emails_list'
email_file=${email_folder}/list.txt
hostname=$(hostname)
emailsend() {
SERVER="smtp.gmail.com"
USER="[email protected]"
PASS="Your_PassWord"
FROM="[email protected]"
TO="${recipient}"
SUBJ="[${hostname}]PM2 process ${process} logs "
MESSAGE="Log File is Attached"
CHARSET="utf-8"
sendemail -f $FROM -t $TO -u $SUBJ -s $SERVER:587 -m $MESSAGE -v -o message-charset=$CHARSET -xu $USER -xp $PASS -a $FILE
}
email_list() {
if [[ ! -d ${email_folder} ]]; then
mkdir -p ${email_folder}
fi
if [[ ! -f ${email_file} ]]; then
touch ${email_file}
fi
email() {
if [[ ${1} == 'add' ]]; then
declare -a emails_list=$(cat "${email_file}")
read -p "Please enter new eMail: " new_email
if cat ${email_file} | grep "^${new_email}" ; then
echo "eMail already exists"
break 1
else
echo "${new_email}" >> "${email_file}"
echo "email added"
declare -a emails_list=$(cat "${email_file}")
break 1
fi
elif [[ ${1} == 'delete' ]]; then
while true; do
declare -a emails_list=$(cat "${email_file}")
emails_list=("ALL" ${emails_list[@]})
PS3='Please Select an eMail to delete: '
select email in ${emails_list[@]}
do
if [[ ${email} == "ALL" ]]; then
rm ${email_file}
touch ${email_file}
echo "All emails deleted"
break 3
else
sed -i "/${email}/d" ${email_file}
echo "email ${email} deleted"
break 3
fi
done
done
fi
}
while true; do
declare -a emails_list=$(cat "${email_file}")
if [[ -z ${emails_list} ]]; then
echo '+--------------------------------------+'
echo '| No Email found, Please Add new email |'
echo '+--------------------------------------+'
fi
options=("Add-eMail" "Delete-eMail" "All" ${emails_list[@]})
PS3='Please Select: '
select option in ${options[@]}
do
if [[ ${option} == "Add-eMail" ]]; then
email add
elif [[ ${option} == "Delete-eMail" ]]; then
email delete
elif [[ ${option} == "All" ]]; then
unset -v 'options[0]'
unset -v 'options[1]'
unset -v 'options[2]'
recipient=${options[@]}
echo "email sent to ${recipient[@]}"
${1}
break 2
else
recipient=${option}
${1}
echo "email sent to ${recipient}"
break 2
fi
done
done
}
logs_file() {
process_list=$(ls ${logs} | sort | awk -F__ '{print $1}' | awk -F. '{print $1}' | uniq)
PS3='Please select the process: '
select process in ${process_list[@]}
do
log_files=$(ls ${logs}/${process}* | awk -F__ '{print $2}' | awk -F. '{print $1}'| sort -r)
PS3='Please select the date: '
logs_files=("TODAY" ${log_files[@]})
select log in ${logs_files[@]}
do
if [[ ${log} == 'TODAY' ]]; then
FILE=${logs}/${process}.log
if [[ ! -z ${1} ]]; then
${1} ${FILE}
break
else
break
fi
else
FILE=${logs}/${process}__${log}.log
if [[ ! -z ${1} ]]; then
${1} ${FILE}
break
else
break
fi
fi
done
break
done
}
main_menu() {
PS3='Would you like to email or view logs?: '
options=("View" "eMail" "Exit")
select option in ${options[@]}
do
if [[ ${option} == "View" ]]; then
logs_file "less"
break
elif [[ ${option} == "eMail" ]];then
logs_file
email_list emailsend
break
elif [[ ${option} == "View-&-eMail" ]];then
logs_file
email_list emailsend
logs_file "less"
break
elif [[ ${option} == "Exit" ]]; then
exit 1
fi
done
}
while true;
do
main_menu
if [[ ${option} == "Exit" ]]; then
exit 1
fi
done