Category Archives: shell

LinuxでIO読み取りおよび書き込み、帯域幅の最大値を取得するサンプルコード

サンプルコード:

iostat -dmx 1 | awk ‘{ if ( $1 !~/(Linux|dm)/ ){
if ($1 != “Device:”)
{
a[$1] = a[$1] < $4 ? $4 : a[$1];
b[$1] = b[$1] < $5 ? $5 : b[$1];
c[$1] = c[$1] < $6 ? $6 : c[$1];
d[$1] = d[$1] < $7 ? $7 : d[$1];
print $1″\t\t”a[$1]”\t\t”b[$1]”\t\t”c[$1]”\t\t”d[$1];
}else{
print $1″\t\t”$4″\t\t”$5″\t\t”$6″\t\t”$7;
}
}
}’

シェル(Shell)でトラフィックを確認する

Shellコード:
ファイル名:traffic.sh

内容下記:
#!/bin/bash
if [ $# -ne 3 ];then
echo Useage : $0 interface interval count
echo Example: $0 eth0 2 10
exit
fi
eth=$1
count=$3
interval=$2
inbytesfirst=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $2}’)
if [ -z “$inbytesfirst” ];then
echo The network interface $eth is not exits!
exit 1;
fi
outbytesfirst=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $10}’)
inpacketsfirst=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $3}’)
outpacketsfirst=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $11}’)
sleep $interval”s”
while [ “1” ]
do

i=0
while [ “$i” -lt “$count” ]
do
inbytesend=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $2}’)
outbytesend=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $10}’)
inpacketsend=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $3}’)
outpacketsend=$(cat /proc/net/dev |tr ‘:’ ‘ ‘|awk ‘/’$eth’/{print $11}’)

bytesin=$(($inbytesend-$inbytesfirst))
bytesout=$(($outbytesend-$outbytesfirst))
packetsin=$(($inpacketsend-$inpacketsfirst))
packetsout=$(($outpacketsend-$outpacketsfirst))

if [ “$bytesin” -lt “0” ];then
bytesin=$((4294967295-$inbytesfirst+$inbytesend))
#echo bytesin $bytesin $inbytesfirst $inbytesend
fi
if [ “$bytesout” -lt “0” ];then
bytesout=$((4294967295-$outbytesfirst+$outbytesend))
#echo bytesout $bytesout $outbytesfirst $outbytesend
fi
if [ “$packetsin” -lt “0” ];then
packetsin=$((4294967295-$inpacketsfirst+$inpacketsend))
#echo packetsin $packetsin $inpacketsfirst $inpacketsend
fi
if [ “$packetsout” -lt “0” ];then
packetsout=$((4294967295-$outpacketsfirst+$outpacketsend))
#echo packetsout $packetsout $outpacketsfirst $outpacketsend
fi

bytesin=$(($bytesin/$interval))
bytesout=$(($bytesout/$interval))
packetsin=$(($packetsin/$interval))
packetsout=$(($packetsout/$interval))

sumbytesin=$(($sumbytesin+$bytesin))
sumbytesout=$(($sumbytesout+$bytesout))
sumpacketsin=$(($sumpacketsin+$packetsin))
sumpacketsout=$(($sumpacketsout+$packetsout))

if [ $(($i%20)) -eq 0 ];then
echo “——— | ———- ———– | ———– ———— | ———— ————-”
echo ” ifname | in_kbits/s out_kbits/s | in_kBytes/s out_kBytes/s | in_packets/s out_packets/s”
echo “——— | ———- ———– | ———– ———— | ———— ————-”
fi
echo $eth $bytesin $bytesout $packetsin $packetsout |awk ‘{printf(“%9s | %10d %11d | %11d %12d | %12d %13d\n”,$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}’
inbytesfirst=$inbytesend
outbytesfirst=$outbytesend
inpacketsfirst=$inpacketsend
outpacketsfirst=$outpacketsend

i=$(($i+1))
sleep $interval”s”
done

sumbytesin=$(($sumbytesin/$i))
sumbytesout=$(($sumbytesout/$i))
sumpacketsin=$(($sumpacketsin/$i))
sumpacketsout=$(($sumpacketsout/$i))

echo “——— | ———- ———– | ———– ———— | ———— ————-”
echo Average $sumbytesin $sumbytesout $sumpacketsin $sumpacketsout |awk ‘{printf(“%9s | %10d %11d | %11d %12d | %12d %13d\n”,$1,$2/128,$3/128,$2/1024,$3/1024,$4,$5)}’
done

シェル(Shell)でmakefileの作成ファイルを指定ディレクトリに入れる

Shellコード:
vpath %.S src/arm
vpath %.c src
vpath %.c src/init

OBJ_PATH = obj
BIN_PATH = bin
SRC_PATH :=src/arm src/init src
SRC= $(foreach x,$(SRC_PATH),$(wildcard $(addprefix $(x)/*,.c .S)))
#OBJ= $(addsuffix .o, $(notdir $(basename $(SRC))))
OBJ = $(addprefix $(OBJ_PATH)/, $(addsuffix .o,$(notdir $(basename $(SRC)))))
ELF=obj/irq_elf
TARGET = irq.bin

$(BIN_PATH)/$(TARGET):$(OBJ)
@echo ‘+++++++++++Make echo: build $(TARGET)’
arm-linux-ld -Ttext 0x00000000 $(OBJ) -o $(ELF)
arm-linux-objdump -D -m arm $(ELF) > $(OBJ_PATH)/irq.dis
arm-linux-objcopy -O binary -S $(ELF) $@

$(OBJ_PATH)/%.o:%.c
arm-linux-gcc -Wall -O2 -c -o $@ $<

$(OBJ_PATH)/%.o:%.S
arm-linux-gcc -Wall -O2 -c -o $@ $<

.PHONY:clean
clean:
rm -f $(BIN_PATH)/* $(OBJ_PATH)/*

Windowsで定時にバックアップされたコードとデータベースファイルを圧縮するスクリプト(bat)

システム要件
タスクマネージャー、7Z圧縮ソフトのインストール

batコード下記:
SET hour=%time:~0,2%
SET hour=%hour: =%
SET DATE=%date:~0,10%_%hour%_%time:~3,2%_%time:~6,2%
SET ZIP_CMD=e:soft-Zipz.exe
set www_root=d:webwww
set svn_root=e:svncode
SET BACKUP_ROOT=e:ak_code
mkdir %BACKUP_ROOT%%USER%_%DATE%
::===============コードバックアップ================
%ZIP_CMD% a -tzip %BACKUP_ROOT%%USER%_%DATE%.www.zip %www_root%
move %BACKUP_ROOT%%USER%_%DATE%.www.zip %BACKUP_ROOT%%USER%_%DATE%

::==============SVNバックアップ===================
%ZIP_CMD% a -tzip %BACKUP_ROOT%%USER%_%DATE%.svncode.zip %svn_root%
move %BACKUP_ROOT%%USER%_%DATE%.svncode.zip %BACKUP_ROOT%%USER%_%DATE%

::====================データベースのバックアップ===============
set DATABASES=db_name1 db_name2 db_name3
:: MySQL ユーザー名
set USERNAME=root
:: MySQL パスワード
set PASSWORD=startnews24
:: MySQL Bin ディレクトリ
:: mysqldumpを利用する
::set MYSQL=D:SERVERMySQLin
::%MYSQL%mysqldump -u%USERNAME% -p%PASSWORD% %%D > %BACKUP_ROOT%%USER%_%DATE%.%%D.sql

echo データベースのバックアップを開始…
for %%D in (%DATABASES%) do (
echo Dumping database %%D …
mysqldump -u%USERNAME% -p%PASSWORD% %%D > %BACKUP_ROOT%%USER%_%DATE%.%%D.sql
::データベースファイルを圧縮
%ZIP_CMD% a -tzip %BACKUP_ROOT%%USER%_%DATE%.%%D.zip %BACKUP_ROOT%%USER%_%DATE%.%%D.sql
move %BACKUP_ROOT%%USER%_%DATE%.%%D.zip %BACKUP_ROOT%%USER%_%DATE%
del %BACKUP_ROOT%%USER%_%DATE%.%%D.sql/Q /S
)
echo Done
exit

shellでHDD使用率の20%以上のデバイスと具体的な使用方法

shellコード:
#!/bin/bash

df | sed 1d > /tmp/startnews24_hddres.txt
#df | grep -v “Use” > /tmp/startnews24_hddres.txt

echo -e “\033[31mDisk Usage(Use > 20%):\033[0m”
while read loop ;do
[ `echo $loop | tr -s ‘ ‘| cut -d ‘ ‘ -f5 | tr -d ‘%’ | grep -v “Use”` -gt 20 ] && \
echo $loop | tr -s ‘ ‘| cut -d ‘ ‘ -f1,5
done < /tmp/res.txt

rm -f /tmp/startnews24_hddres.txt

shellで1~100までの偶数データの合計する

shellコード下記:
#!/bin/bash
# description: the sum even of 1 to 100

#sum変数を定義

declare -i sum=0
let count=0
while [ $count -le 100 ];do
let rst=$count%2
# if [ $rst -eq 0 ];then
# let sum+=$count
#fi
[ $rst -eq 0 ] && let sum+=$count
let count++
done
echo “合計$sum.”

「shell入門」MySQLデータベースのバックアップ設定スクリプト

shellコード下記:
[root@startnews24]# more mysqlfull_startnews24.sh

#!/bin/sh
# Name:mysqlFullBackup.sh
# PS:MySQL DataBase Full Backup.
# Use mysqldump –help get more detail.
scriptsDir=`pwd`
mysqlDir=/var/lib/mysql
user=root
userPWD=Startnews24_mysqlpwd
dataBackupDir=/usr/local/sdc/backup/mysqlbackup
eMailFile=$dataBackupDir/email.txt
eMail=mysqlbackup@arkgame.com
logFile=$dataBackupDir/mysqlbackup.log
DATE=`date -I`
echo “” > $eMailFile
echo $(date +”%y-%m-%d %H:%M:%S”)>>$eMailFile
cd $dataBackupDir
dumpFile=mysql_$DATE.sql
GZDumpFile=mysql_$DATE.sql.tar.gz
#mysqldump -u$user -p$userPWD –opt–default-character-set=utf8 –extended-insert=false –triggers -R –hex-blob–all-databases –flush-logs –delete-master-logs –delete-master-logs -x >$dumpFile
mysqldump -u$user -p$userPWD–all-databases > $dumpFile
#if [[ $? == 0 ]]
if [ “$?” = “0” ]
then
tar czf $GZDumpFile $dumpFile >> $eMailFile 2>&1
echo “BackupFileName:$GZDumpFile” >> $eMailFile
echo “DataBaseバックアップ成功!” >> $eMailFile
rm -f $dumpFile
# Delete daily backup files.
# cd $dataBackupDir/daily
# rm -f *
# Delete old backup files(mtime>33).
#$scriptsDir/rmBackup.sh
find $dataBackupDir -type f -mtime +33 -execrm {} \;
# Move Backup Files To Backup Server.
# if (( !$? )); then
# echo “Move Backup Files To Backup Server Success!” >>$eMailFile
# else
# echo “Move Backup Files To Backup Server Fail!” >>$eMailFile
# fi
else
echo “データベースのバックアップ失敗した!”>> $emailFile
fi
echo”——————————————————–” >>$logFile
cat $eMailFile >> $logFile
#cat $eMailFile | mail -s “MySQLBackup” $eMail

「シェル学習」Zabbixでgeramanのモニタリングを設定するshellプログラム

shellコード下記:
#!/bin/sh
zabbix_server=xxx.xxx.xxx.xxx
my_zabbix_hostname=${Host名_Registered_In_Zabbix}

queue_name=${Queue名前}
interval_seconds=5

while :
do
gearadmin –status | awk ‘
$1==”‘$queue_name'”{
print “‘$my_zabbix_hostname'”, “gearman.total_job_count[‘$queue_name’]”, $2
print “‘$my_zabbix_hostname'”, “gearman.running_job_count[‘$queue_name’]”, $3
print “‘$my_zabbix_hostname'”, “gearman.worker_process_count[‘$queue_name’]”, $4
}’ | zabbix_sender -z $zabbix_server -s $my_zabbix_hostname -i –
sleep $interval_seconds
done

「shell学習」md5sumチェックサムを計算するshellプログラム

shellコード下記:
#!/bin/sh
if [ $# = “2” ] || [ $# = “1” ];
then
md5text=`md5sum $1`
for item in $md5text;
do
break
done
if [ $# = “2” ];
then
if [ “$item” = “$2” ];
then
echo “\033[32m$item\033[0m”
else
echo “\033[31m$item\033[0m”
fi
else
if [ $# = “0” ];
then
md5sum –help
fi
fi
if [ $# = “1” ];
then
echo “\033[36m$item\033[0m”
fi
fi

 

netstatでリッスンしているポートを確認する

C:\Users\startnews24> netstat -ano |find /i “listening”
TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 828
TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
TCP 0.0.0.0:5800 0.0.0.0:0 LISTENING 2748
TCP 0.0.0.0:5900 0.0.0.0:0 LISTENING 2748
TCP 0.0.0.0:9999 0.0.0.0:0 LISTENING 2444
TCP 0.0.0.0:17500 0.0.0.0:0 LISTENING 3688
TCP 0.0.0.0:49152 0.0.0.0:0 LISTENING 524
TCP 0.0.0.0:49153 0.0.0.0:0 LISTENING 924
TCP 0.0.0.0:49154 0.0.0.0:0 LISTENING 1012
TCP 0.0.0.0:49155 0.0.0.0:0 LISTENING 632
TCP 0.0.0.0:49171 0.0.0.0:0 LISTENING 624
TCP 127.0.0.1:5354 0.0.0.0:0 LISTENING 1784
TCP 127.0.0.1:27015 0.0.0.0:0 LISTENING 1660
TCP 192.168.3.13:139 0.0.0.0:0 LISTENING 4
TCP [::]:135 [::]:0 LISTENING 828
TCP [::]:445 [::]:0 LISTENING 4
TCP [::]:49152 [::]:0 LISTENING 524
TCP [::]:49153 [::]:0 LISTENING 924
TCP [::]:49154 [::]:0 LISTENING 1012
TCP [::]:49155 [::]:0 LISTENING 632
TCP [::]:49171 [::]:0 LISTENING 624

4 / 512345