生活随笔
收集整理的這篇文章主要介紹了
puppet系列之nginx+php日志切割与salt结合使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?前面的文章有salt的安裝過程及salt模塊,不清楚的可以去看一下,這篇主要是實(shí)現(xiàn)nginx+php 用logrotate來(lái)對(duì)日志進(jìn)行切割,然后再簡(jiǎn)單的使用下salt這個(gè)工具;
架構(gòu)如下:
?
以下.pp文件均在/etc/puppet/modules/logrotate/manifests下面,內(nèi)容如下
init.pp
?
class?logrotate?(?????????$backup_nginx?????=?"/data/backup/log/nginx",?????????$nginx_log????????=?"/data/log/nginx",?????????$nginx_pid????????=?"/usr/local/nginx/nginx.pid",?????????$base_cron????????=?"/home/cron/logrotate",????????#?$backup_nginx_dir?=?["/data","/data/backup","/data/backup/log","/data/backup/log/nginx"],????????#?$base_cron_dir????=?["/home/cron","/home/cron/logrotate"],?????????$backup_php???????=?"/data/backup/log/php",?????????$php_log??????????=?"/usr/local/php/var/log",?????????$php_pid??????????=?"/usr/local/php/var/run/php-fpm.pid"??){?????????case?$operatingsystem?{?????????????????centos:{?????????????????????????include?logrotate::base?????????????????????????include?logrotate::addcrons?????????????????}?????????}?}? base.pp
?
class?logrotate::base?{?????????package?{"logrotate":???????????ensure?=>?present?????????}?????????????????#??file?{["/data","/data/backup","/data/backup/log","/data/backup/log/nginx"]:??????????file?{"$backup_nginx":???????????ensure??=>?present,?????????}????????#??file?{["/home/cron","/home/cron/logrotate"]:??????????file?{"$base_cron":???????????ensure??=>?present,?????????}??????????????????file?{"$backup_php":???????????ensure??=>?present?????????}?}? addcrons.pp
class?logrotate::addcrons?{?????????file?{"logrotate_nginx_conf":???????????owner???=>?root,???????????group???=>?root,???????????mode????=>?644,???????????path????=>?"/home/cron/logrotate/logrotate_nginx.conf",???????????content?=>?template('logrotate/logrotate_nginx_conf.erb'),???????????require?=>?Class["logrotate::base"]?????????}??????????file?{"logrotate_nginx_sh":???????????owner???=>?root,???????????group???=>?root,???????????mode????=>?644,???????????path????=>?"/home/cron/logrotate/logrotate_nginx.sh",???????????content?=>?template('logrotate/logrotate_nginx_sh.erb'),???????????require?=>?Class["logrotate::base"]?????????}??????????cron?{"logrotate?nginx":???????????command???=>?"bash?/home/cron/logrotate/logrotate_nginx.sh",???????????user??????=>?root,???????????minute????=>?59,???????????hour??????=>?23,???????????require???=>?File['logrotate_nginx_conf','logrotate_nginx_sh']?????????}??????????file?{"logrotate_php_conf":???????????owner???=>?root,???????????group???=>?root,???????????mode????=>?644,???????????path????=>?"/home/cron/logrotate/logrotate_php.conf",???????????content?=>?template('logrotate/logrotate_php_conf.erb'),???????????require?=>?Class["logrotate::base"]?????????}??????????file?{"logrotate_php_sh":???????????owner???=>?root,???????????group???=>?root,???????????mode????=>?644,???????????path????=>?"/home/cron/logrotate/logrotate_php.sh",???????????content?=>?template('logrotate/logrotate_php_sh.erb'),???????????require?=>?Class["logrotate::base"]?????????}??????????cron?{"logrotate?php":???????????command???=>?"bash?/home/cron/logrotate/logrotate_php.sh",???????????user??????=>?root,???????????minute????=>?59,???????????hour??????=>?23,???????????require???=>?File['logrotate_php_conf','logrotate_php_sh']?????????}??}? =================================
以下是四個(gè).erb文件的內(nèi)容
logrotate_nginx_conf.erb
<%=?nginx_log?%>/*.log?{?????daily?????rotate?7?????missingok?????dateext?????notifempty?????compress?????sharedscripts?????postrotate?????[?!?-f?<%=?nginx_pid?%>?]?||?/bin/kill?-USR1?`/bin/cat?<%=?nginx_pid?%>`?????endscript?????}? logrotate_nginx_sh.erb
#!/bin/bash??BACKUP_DIR=<%=?backup_nginx?%>??#?nginx?備份日志目錄?NGINX_LOG_PATH=<%=?nginx_log?%>??????#?nginx?原始日志目錄?LOGROTATE_CONF_DIR=`dirname?$0`????#?logrotate?配置文件目錄,默認(rèn)跟本腳本同目錄??#?切割日志?/usr/sbin/logrotate?-f?$LOGROTATE_CONF_DIR/logrotate_nginx.conf??if?[?!?-d?$BACKUP_DIR?];?then?????mkdir?-p?$BACKUP_DIR?fi??#?對(duì)切割完的日志直接重定向壓縮到?BACKUP_DIR?for?log?in?`ls?$NGINX_LOG_PATH/*-20[1-9][0-9][0,1][0-9][0-3][0-9].gz`;?do?????????mv?$log?$BACKUP_DIR/?done??#?刪除7天前的?nginx?備份日志?find?$BACKUP_DIR/*?-name?"*.gz"?-mtime?7?-type?f?-exec?rm?-f?{}?\;? logrotate_php_conf.erb
<%=?php_log?%>/*.log?{????daily????rotate?7????missingok????dateext????notifempty????compress????sharedscripts????postrotate????[?!?-f?<%=?php_pid?%>?]?||?/bin/kill?-USR1?`/bin/cat?<%=?php_pid?%>`????endscript????}? logrotate_php_sh.erb
#!/bin/bash??BACKUP_DIR=<%=?backup_php?%>?????????#?php?日志備份目錄?PHP_LOG_PATH=<%=?php_log?%>??????????????#?php?日志原始目錄?LOGROTATE_CONF_DIR=`dirname?$0`?????????#?logrotate?配置文件目錄,默認(rèn)跟本腳本同目錄??#?切割日志?/usr/sbin/logrotate?-f?$LOGROTATE_CONF_DIR/logrotate_php.conf??if?[?!?-d?$BACKUP_DIR?];?then?????mkdir?-p?$BACKUP_DIR?fi??#?對(duì)切割完日志進(jìn)行重定向壓縮到?BACKUP_DIR?for?log?in?`ls?$PHP_LOG_PATH/*-20[1-9][0-9][0,1][0-9][0-3][0-9].gz`;?do?????????mv?$log?$BACKUP_DIR/?done??#?刪除7天前的備份日志?find?$BACKUP_DIR/*?-name?"*.gz"?-mtime?7?-type?f?-exec?rm?-f?{}?\;? 由base.pp可知我們是需要backup_nginx,backup_php,base_cron這些參數(shù)的目錄,這個(gè)我們可以直接用salt命令來(lái)實(shí)現(xiàn)
?
salt 'slave.domain.com' cmd.run 'mkdir -p?/data/backup/log/nginx&&?/data/backup/log/php&&?mkdir -p /home/cron/logrotate' salt 'slave.domain.com' cmd.run 'puppet agent --test --server func.domain.com'? 下篇待續(xù)
?
轉(zhuǎn)載于:https://blog.51cto.com/tntdba/1147937
總結(jié)
以上是生活随笔為你收集整理的puppet系列之nginx+php日志切割与salt结合使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。