forked from y123456yz/reading-code-of-nginx-1.9.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iconv_transfer
executable file
·265 lines (202 loc) · 5.16 KB
/
iconv_transfer
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/bin/sh
#=====================
# configure
#=====================
function fun_dir(){
for child in `ls $1`
do
if [[ -f $1/$child ]]
then
echo "convert $1/$child"
iconv -f utf-8 -t gb2312 $1/$child > $2/$child
if (( $? != 0 ))
then
cp $1/$child $2/$child
echo "cp $1/$child $2/$child"
fi
elif [[ -d $1/$child ]]
then
mkdir $2/$child
fun_dir $1/$child $2/$child
fi
done
}
if (( $# != 2 ))
then
echo "parameter error"
exit
fi
fun_dir ${1%/} ${2%/}
exit
# check period (second)
CHECK_PERIOD=10
# the proccess name needed to checking
PROC_NAME_LIST="mcpd mcplog nc safagent"
# number of connections
NO_OF_CONNECTIONS=6
# System CPU used
CPU_USED=70
# where message print
OUT_TTY="/dev/ttyS0"
#OUT_TTY=""
#=====================
# function
#=====================
function outlog()
{
MYDATE=`date +%Y%m%d-%H:%M:%S`
echo ${MYDATE} "Warning: "$1 >>/var/log/start_mu.log
}
####start process, arg1 is process name ###
function start_proc()
{
case "$1" in
'mcpd')
if [ -x /usr/local/bin/mcpd ]; then
/usr/local/bin/mcpd -d -f /usr/local/conf/mcpd.conf -i /var/run/mcpd.pid >/dev/null
fi
;;
'nc')
if [ -x /usr/local/bin/nc ]; then
/usr/local/bin/nc -D -f /usr/local/conf/mcpprobd.conf >/dev/null
fi
;;
'safagent')
if [ -x /usr/local/bin/safagent ]; then
/usr/local/bin/safagent -f /usr/local/conf/config.xml >/dev/null &
fi
;;
'mcplog')
if [ -x /usr/local/bin/mcplog ]; then
/usr/local/bin/mcplog -d 1 -l /var/log/mcp_log_db_err.log -D >/dev/null
fi
;;
'httpd')
if [ -x /usr/local/bin/apachectl ]; then
/usr/local/bin/apachectl start >/dev/null
fi
;;
'mysqld')
if [ -x /etc/rc.d/rc.mysqld ]; then
/etc/rc.d/rc.mysqld start >/dev/null
fi
;;
'snort')
if [ -x /etc/rc.d/rc.snort ]; then
/etc/rc.d/rc.snort start >/dev/null
fi
;;
'check_pkt')
if [ -x /usr/local/bin/check_pkt ]; then
/usr/local/bin/check_pkt -d -i /var/run/check_pkt.pid >/dev/null
fi
;;
'netflow')
if [ -x /usr/local/bin/netflow ]; then
/usr/local/bin/netflow bond0 1 & >/dev/null
fi
;;
'dbwriter')
if [ -x /usr/local/bin/dbwriter ]; then
/usr/local/bin/dbwriter -f /usr/local/conf/ids_cfg.txt & >/dev/null
fi
;;
'yhids')
if [ -x /usr/local/bin/yhids ]; then
/usr/local/bin/yhids -i eth2 -c /usr/local/conf/yhids/yhids.conf -A udp -j -J & >/dev/null
fi
;;
*)
return 1
esac
}
####check process and start
function check_proc_start()
{
pname=$1
proc_pid=`pidof ${pname}`
if [ "$proc_pid" == "" ]; then
start_proc ${pname}
proc_pid=`pidof ${pname}`
if [ "$proc_pid" == "" ]; then
outlog "process [${pname}] has stoped, can NOT start!"
else
outlog "process [${pname}] has stoped, and have restart!"
fi
fi
}
function check_net_connection()
{
tmpfile=/tmp/NETIP_LIST
tmpnum=/tmp/NETIP_NUMLIST
tmpout=/tmp/NETIP_OUTLIST
netstat -ntp | grep "^tcp" >$tmpfile
cat $tmpfile| awk '{print $7}' | sort | uniq -c | sort -nr >$tmpnum
while read line; do
cur_line_num=$(echo $line | cut -d" " -f1)
cur_proc=$(echo $line | cut -d" " -f2)
if [ $cur_line_num -lt $NO_OF_CONNECTIONS ]; then
break
fi
outlog "proccess [${cur_proc}] with $cur_line_num connections"
if [ $cur_proc == "-" ]; then
cat $tmpfile|grep "-"|awk '{print $6}'| awk '{++S[$NF]} END {for(a in S) print a, S[a]}' >$tmpout
else
cat $tmpfile | grep "$cur_proc" >$tmpout
fi
done < $tmpnum
rm -f $tmpfile
rm -f $tmpnum
rm -f $tmpout
}
function check_system_cpu()
{
tmptop=/tmp/TMP_TOPFILE
tmpout=/tmp/TMP_OUTTOPFILE
cpu_used=0
echo "PID USER %CPU COMMAND" >$tmpout
top -bn 1 | grep "^ *[1-9]" | awk '{if($9>0)print $1,$2,$9,$12}'|sed 's/ /\t/g' >$tmptop
while read line; do
proc_cpu=`echo $line | awk '{print $3}'|grep -v '0.0'`
if ! [ "$proc_cpu" = "" ]; then
cpu_used=`echo "$cpu_used + $proc_cpu"|bc`
echo $line|sed 's/ /\t/g' >>$tmpout
fi
done < $tmptop
tmp_use=`echo "$cpu_used>$CPU_USED"|bc`
[ $tmp_use -eq 1 ] && outlog "System CPU($CPU_NUM) used: "$cpu_used"%"
rm -f $tmptop
rm -f $tmpout
}
#add core dump file directory, then change the core dump file address
tmp_dir=`pwd`
if [ ! -d /var/corefile ] ; then
cd /var/
mkdir corefile
fi
cd $tmp_dir
core_dump_times=0 #change directory of the coredump file after 2 minutes
##################################
#
# Main loop
#
##################################
[ "$1" != "" ] && PROC_NAME_LIST=$1
CPU_NUM=`cat /proc/cpuinfo | grep 'processor' |wc -l`
while true
do
for onename in `echo ${PROC_NAME_LIST}`; do
check_proc_start $onename
done
# check_net_connection
# check_system_cpu
sleep ${CHECK_PERIOD}
if [ $core_dump_times -le 12 ] ; then
core_dump_times=`expr $core_dump_times + 1`
if [ $core_dump_times -eq 12 ] ; then
ulimit -c unlimited
echo 0 > /proc/sys/kernel/core_uses_pid
echo /var/corefile/core-%e > /proc/sys/kernel/core_pattern
fi
fi
done