# apt-get install sysstat
# vim /etc/default/sysstat
# vim /etc/cron.d/sysstat
# Activity reports every 1 minutes everyday * * * * * root command -v debian-sa1 > /dev/null && debian-sa1 1 1 # Additional run at 23:59 to rotate the statistics file 59 23 * * * root command -v debian-sa1 > /dev/null && debian-sa1 60 2
# service sysstat restart
$ sar -s 07:00:00 -e 07:10:00 Linux 3.2.0-4-amd64 (mind-x) 02.03.2015 _x86_64_ (1 CPU) 07:00:01 CPU %user %nice %system %iowait %steal %idle 07:01:01 all 0,64 0,00 0,15 0,10 0,00 99,11 07:02:01 all 0,03 0,00 0,02 0,00 0,00 99,95 07:03:01 all 0,03 0,00 0,02 0,00 0,00 99,95 07:04:01 all 0,03 0,00 0,02 0,02 0,00 99,93 07:05:01 all 0,05 0,00 0,03 0,00 0,00 99,92 07:06:01 all 0,63 0,00 0,17 0,10 0,00 99,11 07:07:01 all 0,03 0,00 0,02 0,00 0,00 99,95 07:08:01 all 0,02 0,00 0,02 0,00 0,00 99,97 07:09:01 all 0,03 0,00 0,02 0,00 0,00 99,95 : all 0,17 0,00 0,05 0,02 0,00 99,76
CREATE DATABASE `sysstat`;
CREATE USER 'sysstat'@'localhost' IDENTIFIED BY 'some_pass'; GRANT ALL PRIVILEGES ON sysstat.* TO 'sysstat'@'localhost';
CREATE TABLE `host_health_cpu` ( `datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `pct_user` decimal(10,2) DEFAULT NULL, `pct_nice` decimal(10,2) DEFAULT NULL, `pct_system` decimal(10,2) DEFAULT NULL, `pct_iowait` decimal(10,2) DEFAULT NULL, `pct_steal` decimal(10,2) DEFAULT NULL, `pct_idle` decimal(10,2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `host_health_memory` ( `datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `kbmemfree` int(11) DEFAULT NULL, `kbmemused` int(11) DEFAULT NULL, `per_memused` decimal(10,2) DEFAULT NULL, `kbbuffers` int(11) DEFAULT NULL, `kbcached` int(11) DEFAULT NULL, `kbcommit` int(11) DEFAULT NULL, `per_commit` decimal(10,2) DEFAULT NULL, `kbactive` int(11) DEFAULT NULL, `kbinact` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `host_health_la` ( `datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `runq_sz` int(11) DEFAULT NULL, `plist_sz` int(11) DEFAULT NULL, `ldavg_1` decimal(10,2) DEFAULT NULL, `ldavg_5` decimal(10,2) DEFAULT NULL, `ldavg_15` decimal(10,2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `host_health_net` ( `datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `iface` varchar(7) DEFAULT NULL, `rxpck_persec` decimal(10,2) DEFAULT NULL, `txpck_persec` decimal(10,2) DEFAULT NULL, `rxbyt_persec` decimal(10,2) DEFAULT NULL, `txbyt_persec` decimal(10,2) DEFAULT NULL, `rxcmp_persec` decimal(10,2) DEFAULT NULL, `txcmp_persec` decimal(10,2) DEFAULT NULL, `rxcst_persec` decimal(10,2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
#!/bin/bash # WORKDIR=/var/cache/sar FMATDIR=/var/cache/sar_data SADF=`which sadf` HEAD=`which head` AWK=`which awk` # cd ${WORKDIR} COPY=`which cp` SAR_LOG=/var/log/sysstat cd ${SAR_LOG} LATEST_DATA=`ls -tlr ${SAR_LOG} | tail -1 | awk '{print $9}'` ${COPY} ${LATEST_DATA} ${WORKDIR} for file in *; do # 6 . $TIME UNIXTIME=$((`date +%s`-300)) TIME=`date -d@${UNIXTIME} +%H:%M:%S` # CPU ${SADF} -d ${file} -s ${TIME} -t | cut -d ';' -f3,5- > "${FMATDIR}"/"${file}"-host_health_cpu.csv # ${SADF} -d ${file} -s ${TIME} -t -- -n DEV | cut -d ';' -f3- > "${FMATDIR}"/"${file}"-host_health_net.csv # LA ${SADF} -d ${file} -s ${TIME} -t -- -q | cut -d ';' -f3- > "${FMATDIR}"/"${file}"-host_health_la.csv # ${SADF} -d ${file} -s ${TIME} -t -- -r | cut -d ';' -f3- > "${FMATDIR}"/"${file}"-host_health_memory.csv done # MySQL cd ${FMATDIR} MYSQL=`which mysql` USER='user' PASS='some_pass' HOST='localhost' DB='sysstat' for file in *.csv; do ${MYSQL} -u${USER} -p${PASS} -h${HOST} -D${DB} -e "LOAD DATA LOCAL INFILE '${FMATDIR}/${file}' INTO TABLE `echo ${file} | sed 's/.csv//g' | awk -F'-' '{print $2}'` FIELDS TERMINATED BY ';' IGNORE 1 LINES;" done # rm -fr ${FMATDIR}/*.csv rm -fr ${WORKDIR}/sa*
*/5 * * * * bash /usr/local/sbin/sar.sh
mysql> SELECT * FROM host_health_memory WHERE datetime > NOW() - INTERVAL 10 MINUTE; +---------------------+-----------+-----------+-------------+-----------+----------+----------+------------+----------+---------+ | datetime | kbmemfree | kbmemused | per_memused | kbbuffers | kbcached | kbcommit | per_commit | kbactive | kbinact | +---------------------+-----------+-----------+-------------+-----------+----------+----------+------------+----------+---------+ | 2015-03-02 08:36:01 | 1381896 | 679396 | 32.00 | 104044 | 155520 | 803420 | 38.00 | 484244 | 142688 | | 2015-03-02 08:37:01 | 1377476 | 683816 | 33.00 | 104068 | 155668 | 810284 | 39.00 | 487632 | 142808 | | 2015-03-02 08:38:01 | 1377476 | 683816 | 33.00 | 104096 | 155672 | 810284 | 39.00 | 487668 | 142804 | | 2015-03-02 08:39:01 | 1377476 | 683816 | 33.00 | 104120 | 155680 | 810524 | 39.00 | 487832 | 142804 | | 2015-03-02 08:40:01 | 1372416 | 688876 | 33.00 | 104160 | 155684 | 819104 | 39.00 | 490708 | 142816 | | 2015-03-02 08:41:01 | 1377104 | 684188 | 33.00 | 104276 | 155700 | 810524 | 39.00 | 488008 | 142808 | | 2015-03-02 08:42:01 | 1379228 | 682064 | 33.00 | 104288 | 155708 | 816640 | 39.00 | 486392 | 142632 | | 2015-03-02 08:43:01 | 1378980 | 682312 | 33.00 | 104328 | 155708 | 816744 | 39.00 | 486680 | 142628 | | 2015-03-02 08:44:01 | 1378608 | 682684 | 33.00 | 104356 | 155716 | 816932 | 39.00 | 486936 | 142636 | | 2015-03-02 08:45:01 | 1371564 | 689728 | 33.00 | 104392 | 155720 | 827704 | 40.00 | 491912 | 142648 | +---------------------+-----------+-----------+-------------+-----------+----------+----------+------------+----------+---------+
#!/bin/bash sleep 10 # width="640" high="480" outfile="graph.svg" format="svg" NO_ARGS=0 E_OPTERROR=65 if [ $# -eq "$NO_ARGS" ] # ? then echo " ! : -o ( \"graph.svg\") -f ( \"svg\") -w ( \"640\") -h ( \"480\") -t ." exit $E_OPTERROR fi # while getopts ":h:w:t:f:o:" Option do case $Option in h) high="$OPTARG";; w) width="$OPTARG";; f) format="$OPTARG";; o) outfile="$OPTARG";; t) type="$OPTARG";; esac done shift $(($OPTIND - 1)) # /tmp/datatmp if [ ${type} == 'cpu' ] then # title=" CPU, %" QUERY="select time(datetime), pct_idle as idle, pct_iowait as iowait, pct_system as system, pct_user as user from host_health_cpu where now() - interval 1 hour < datetime;" format_y='set format y "%.0f%%";' elif [ ${type} == 'mem' ] then title=" , B" QUERY="select time(datetime),(kbmemfree * 1024) as Free,(kbmemused * 1024) as Used,(kbbuffers * 1024) as Buffers,(kbcached * 1024) as Cached from host_health_memory where now() - interval 1 hour < datetime;" format_y="set format y '%.1s%cB'" elif [ ${type} == "net" ] then title=" , B/s" QUERY='select time(datetime), (rxbyt_persec * 1024) as Rx, (txbyt_persec * 1024) as Tx from host_health_net where now() - interval 1 hour < datetime and iface="eth0";' format_y="set format y '%.1s%cB/s'" elif [ ${type} == "la" ] then title="Load Average" QUERY="select time(datetime),ldavg_1 as LoadAvg1, ldavg_5 LoadAvg5,ldavg_15 as LoadAvg15 from host_health_la where now() - interval 1 hour < datetime;" format_y="set format y '%.2f'" fi # MYSQL=`which mysql` USER="user" PASS="some_pass" DB="sysstat" ${MYSQL} -u${USER} -p${PASS} -D${DB} -B -r -e "${QUERY}" >> /tmp/datatmp_${type} # . cols=`awk '{print NF}' /tmp/datatmp_${type} | sort -nu | tail -n 1` # hour_ago=`${MYSQL} -u${USER} -p${PASS} -D${DB} -N -e "select time(datetime) from host_health_cpu where now() - interval 1 hour < datetime limit 1;"` now=`${MYSQL} -u${USER} -p${PASS} -D${DB} -N -e "select time(datetime) from host_health_cpu where now() - interval 1 hour < datetime order by datetime desc limit 1;"` # gnuplot << EOP # set terminal ${format} size ${width},${high} # set output ${outfile} # set key autotitle columnhead set key outside center bottom set key horizontal # set style fill transparent solid 0.5 noborder set title "${title}" # set xdata time set timefmt "%H:%M:%S" set xrange ["${hour_ago}":"${now}"] set xtics format "%H:%M" # set xlabel "" set ylabel "${title}" set grid set yrange [0:*] ${format_y} # . plot for [i=2:${cols}] "/tmp/datatmp_${type}" using 1:i smooth unique with filledcurve x1 EOP # . rm /tmp/datatmp_${type} echo "Image write to \"${outfile}\"" exit 0;
$ sar-plot.sh -t mem -o /path/to/memory.svg
*/5 * * * * bash /usr/local/sbin/sar-plot.sh -t cpu -o /path/to/images/cpu.svg */5 * * * * bash /usr/local/sbin/sar-plot.sh -t mem -o /path/to/images/memory.svg */5 * * * * bash /usr/local/sbin/sar-plot.sh -t net -o /path/to/images/network.svg */5 * * * * bash /usr/local/sbin/sar-plot.sh -t la -o /path/to/images/la.svg
Source: https://habr.com/ru/post/252201/
All Articles