设为首页 收藏本站
查看: 1644|回复: 0

[经验分享] squid缓存系统全攻略

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-19 10:14:14 | 显示全部楼层 |阅读模式
转自:http://blog.iyunv.com/uid-16909016-id-120110.html1.操作系统的选择
操作系统的版本为Red Hat AS4 update5版本。系统选择最小安装。开启防火墙,防火墙开启ssh及www两项服务端口,在安装系统的时候关掉SELinux;系统分区时创建三个交换分区,每个交换分区的大小为实际物理内存的2倍,三个交换分区的优先级相同。
# more /var/log/messages //检查有无系统内核级错误信息# demesg //检查硬件设备是否有错误信息# ifconfig //检查网卡设置是否正确# ping www.163.com // 检查网络是否正常
# setup //选择启动的服务进入system service 选项。以space 键选定所需服务。以下仅列出需要启动的服务,未列出的服务一律关闭:crondmicrocode_ctlnetworkntpdiptablessshdsyslog5.     定时校正服务器时间# crontab –e0 4 * * * /usr/sbin/ntpdate 210.72.145.44以上命令设置好后存盘。# /sbin/service crond reload机器将在每天的4:00根据中国国家授时中心的NTP服务器时间自动校准时间。  二、   Apache的安装1.       Apache版本选择服务器Apache选择2.2版,2.2版的Apache能在混合多进程、 多线程模式下运行,使很多配置的可扩缩性得到改善。2.       Apache编译安装默认情况下,apache安装完以后,是不允许被cache的。如果外接了cache或squid服务器要求进行web加速的话,就需要在httpd.conf里进行设置,当然前提是在安装apache的时候要激活mod_cache的模块。
安装与配置的步骤
apache 编译配置:# ./buildconf
./configure \
--with-mpm=worker \
--enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache \
--enable-mods=max \--enable-mod-share=most参数 -with-mpm={prefork,worker},这个是设置Apache的多任务处理的。prefork是指每个连接一个进程,也就是多进程并行处理;而worker是多线程并行处理。支持多线程处理是apache 2的一大改进。#make#make install检验是否安装了cache模块:/usr/local/apache2/bin/apachectl –l如果有mod_cache.cmod_disk_cache.cmod_mem_cache.c这3个模块,说明该apache已经可以支持cache。3.       Apache配置更改httpd.conf的设置vi /usr/local/apache2/conf/httpd.conf在末尾添加上:# 启用有效期控制expiresactive onExpiresByType text/html A1800          # html 文件有效期为30分钟ExpiresByType text/htm A1800              # htm 文件有效期为30分钟ExpiresByType text/shtml A1800         # shtml 文件有效期为30分钟ExpiresByType image/gif A86400         # gif文件有效期为一天ExpiresByType image/jpeg A86400        # jig 文件有效期为一天ExpiresByType image/png A86400         # png文件有效期为一天ExpiresByType text/css A86400          # css文件有效期为一天ExpiresByType text/js A86400              # js文件有效期为一天
设置虚拟目录
然后保存退出。启动apache
参数的解释:
ExpiresByType 在apache应答时在网页文件添加http头,以控制文档的有效性和持久性。# 启用有效期控制 ExpiresActive On
# GIF有效期为1个月
ExpiresByType image/gif A2592000
# HTML文档的有效期是最后修改时刻后的一星期
ExpiresByType text/html M14404800"M"表示源文件的最后修改时刻,"A"表示客户端对源文件的访问时刻。如果使用"M",所有缓存服务器中的当前同一文档副本都将在同一时刻过期。如果使用"A",则每个客户端所得到的有效期是不一样的,如果设定文件过期时间为10分钟,当地一个用户在早上8点访问该文件,该文件被缓存后,默认十分钟过期,即该文件将在8:10过期,第二个用户于8:09访问该文件,那么该文件的过期时间将从8:09分开始计算,文件缓存十分钟,文件将在8:19分过期。使用了“A”,表明Apache控制文件过期采用了客户端访问该文件最后的时间加上文件的有效期作为过期时间。                       三、   squid的安装和设置1.       squid版本选择本方案选则squid的版本为squid-2.6.STABLE17,并采用二进制包安装。2.       squid的编译安装解压缩squid源码包,并进行编译安装:#tar xzvf squid-2.6.tar.gz
#cd squid-2.6./configure --prefix=/usr/local/squid --enable-dlmalloc --with-pthreads --enable-poll \ --disable-internal-dns   --enable-stacktrace --enable-removal-policies="heap,lru" \ --enable-delay-pools --enable-storeio="aufs,coss,diskd,ufs" --enable-arp-acl \ --enable-err-language="Simplify_Chinese"--enable-underscore--enable-snmp\ --enable-default-err-languages="Simplify_Chinese"    --enable-linux-netfilter \ --disable-ident-lookups --enable-async-io=80编译参数解释:--enable-cache-digests 使用缓存摘要,此项目的是为了在Squid集群服务之间迅速发现缓存对象,在本地使用,可以加快请求时,检索缓存内容的速度。
--enable-cahce-digests  :加快请求时检索缓存内容的速度。
--enable-snmp 使Squid支持SNMP接口
--enable-async-io=80 :这个主要是设置使用异步模式来运行squid,--enable-icmp  :加入icmp支持
--enable-kill-parent-hack :关掉suqid的时候,要不要连同父进程一起关掉--enable-poll  :应启用Poll (轮询)函数而不是select函数--enable-linux-netfilter :可以支持透明代理
--enable-underscore  :允许解析的URL中出现下划先,因为默认squid会认为带下划线的URL地址是非法的,并拒绝访问该地址。 #make#make installSquid安装完成了。3.        squid参数配置  # vi  squid.conf  代码如下:#监听服务器的80端口,反向代理,支持域名和IP的虚拟主机http_port 219.234.80.151:80 vhost vport   
#将icp通讯端口设置为3130
icp_port 3130     
#设置源Web服务器的ip和端口
cache_peer 219.234.80.141 parent 80 0 no-query originserver no-digest name=cache0   
#设置web服务器优先级第二的源地址及端口
cache_peer 219.234.80.142parent 80 0 no-query originserver no-digest name=cache1   
#设置web服务器优先级第三的源地址及端口
#配置本机squid允许接受访问的域名,请注意name的对应cache_peer 219.234.80.143 parent 80 0 no-query originserver no-digest name=cache2 注:以上源服务器优先级配置为cache1的优先级配置,cache2上的优先级配置如下:cache_peer 219.234.80.142  parent 80 0 no-query originserver no-digest name=cache0cache_peer 219.234.80.143  parent 80 0 no-query originserver no-digest name=cache1cache_peer 219.234.80.141  parent 80 0 no-query originserver no-digest name=cache2以下参数为共同参数:#允许以下端口的代理
acl Safe_ports port 80
acl Safe_ports port 3130
#Squid信息设置visible_hostname www.lotour.comcache_mgr admin@lotour.com#squid使用的用户组和用户名cache_effective_user nobodycache_effective_group nobody#tcp receive buffer sizetcp_recv_bufsize 65535 bytesclient_persistent_connections offserver_persistent_connections on#for security set half_closed_clients offhalf_closed_clients off#设置Squid所能使用的内存共1024MB,这个&#20540;因人而异,缓存设置应为物理缓存的25%,当缓存空间使用达到95%时新的内容将取代旧的而不直接添加到目录中,直到空间又下降到90%才停止这一活动cache_mem 1024MBcache_swap_low 90cache_swap_high 95#设置缓存服务器中缓存文件的大小,在设定数&#20540;范围外的文件将不被缓存。maximum_object_size 4096 KBminimum_object_size 0 KB  maximum_object_size_in_memory 4096 KB  #设定缓存服务器中内存中可以缓存的文件最大&#20540;ipcache_size 1024ipcache_low 90ipcache_high 95cache_replacement_policy lru  #磁盘缓存置换策略使用最近最少使用memory_replacement_policy lru  #内存置换策略使用最近最少使用算法#磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是10240MB,都是 16* 256 级子目录cache_dir ufs  /cache0 10240 16 256cache_dir ufs  /cache1 10240 16 256#这个设置是不记录store.log (记录储存管理的活动)cache_store_log none#日志保留最近30个文件,每天轮询三次,即保留最近十天的内容.logfile_rotate  30#设置默认刷新规则,关于refresh_pattern参数的解释见备注refresh_pattern -i .html 1440 50% 10080 reload-into-imsrefresh_pattern -i .htm 1440 50% 10080 reload-into-imsrefresh_pattern -i .shtml 1440 50% 10080 reload-into-imsrefresh_pattern -i .png 4320  50% 20160 reload-into-imsrefresh_pattern -i .gif 4320  50% 20160 reload-into-imsrefresh_pattern -i .bmp 4320  50% 20160 reload-into-imsrefresh_pattern -i .jpg 4320  50% 20160 reload-into-imsrefresh_pattern -i .js 4320  50% 20160 reload-into-imsrefresh_pattern -i .css 4320  50% 20160 reload-into-imsrefresh_pattern -i .swf 4320  50% 20160 reload-into-ims#不要相信ETag 因为有gzipacl apache rep_header Server ^Apachebroken_vary_encoding allow apache#设置超时策略以节省服务器资源#设置squid多长时间为请求寻找转发的路径,超过设定时间放弃转发.forward_timeout 20 seconds#设置squid多长时间等待TCP连接到请求服务器或者邻居服务器,时间超过此&#20540;Squid试图寻找转发请求的路径.connect_timeout 15 seconds#应用于服务器端连接,每次成功读取数据之后,timeout就设置为这个&#20540;.如果这个设置时间之后在所设定的时间内没有数据再被读,请求就中止.read_timeout 3 minutes#初始化连接建立后,多长时间去等待HTTP请求.request_timeout 1 minutes#先前的请求完成后,在一个持续的连接中多长时间去等待下一个HTTP请求persistent_request_timeout 15 seconds#允许一个客户保持连接到缓存进程的最大保留时间&#20540;.client_lifetime 15 minutes#关闭squid时,缓存程序处于关闭等待状态,当接收到关闭命令后,在设定的时间过后向用户返回超时信息。shutdown_lifetime 5 seconds#设置网页错误信息在缓存中的保留时间negative_ttl 10 seconds#打开emulate_httpd_log选项,将使Squid仿照Aapche的日志&#26684;式emulate_httpd_log on#日志&#26684;式combined的设置logformat combined %>a %ui %un [%tl] &quot;%rm %ru HTTP/%rv&quot; %Hs %<st &quot;%{Referer}>h&quot; &quot;%{User-Agent}>h&quot; %Ss:%Sh#这里是设置pid和日志文件的位置,因人而异,同时日志&#26684;式是combined,awstats可以直接调用分析了pid_filename /usr/local/squid/var/squid.pidcache_log /usr/local/squid/var/logs/cache.logaccess_log /usr/local/squid/var/logs/access.log combined#设置不想缓存的目录或者文件类型acl all src 0.0.0.0/0.0.0.0acl QUERY  urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip.exe.aspx .aspcache deny QUERY#错误文档error_directory /usr/local/squid/share/errors/Simplify_Chinese#允许没有被明确禁止的客户进行访问。http_access allow all 保存配置文件退出。初始化和启动squid
# /usr/local/squid/sbin/squid -z
# /usr/local/squid/sbin/squid -NCd1
第一条命令是先初始化squid缓存哈希子目录,只需执行一次即可。启动squid:./bin/RunCache &使用squid附带的启动脚本来启动squid,有个好处就是,如果squid的进程死了的话,这个脚本会自动启动squid,对于运行在线上的服务器来说,这点很重要了。
开机自动运行squid
    修改开机自动运行文件,将 /usr/local/squid/bin/RunCache & 加进开机自动执行的程序中。第一次是以 root 身份启动,所以,/usr/local/squid/var/squid.out 及 /usr/local/squid/var/squid.pid 的拥有者是 root ,这会造成下列以 nobody 身份启动 squid 时,会有错误出现,所以,必须在第二次由 rc.local 启动前,先行将 squid.out 及 squid.pid 两个文件的所有者(owner)改为 nobody,即是
chown nobody:nobody /usr/local/squid/var/squid.out
chown nobody:nobody /usr/local/squid/var/logs/squid.pid
    在 /etc/rc.d/rc.local 中加入 /usr/local/squid/bin/RunCache &4.       第二机房缓存服务器配置文件第二机房缓存服务器配置文件与第一机房缓存服务器配置文件基本相同,只需修改如下参数:#设置源Web服务器的ip和端口
cache_peer源服务器地址parent 80 0 no-query originserver no-digest name=cache0#连接B服务器的Squid工作在sibling模式并指定其端口
cache_peer 第二机房主缓存服务器地址 sibling 80 3130 name=cache1   

缓存服务器之间的连接工作在Sibling模式,当Squid Server工作在Sibling模式时。当Squid Server没有资料时会先向Sibling连接的Squid Server要资料,如果Sibling没资料,就跳过它直接到internet 源服务器去获取。 四、   配置参数及选项的设置 参数参数选项&#20540;默认参数&#20540;建议参数&#20540;建议参数选项&#20540;建议原因http_port3128vhost vport80vhost vport设置80端口是因为服务器要提供web加速,使用vhostvport使缓存服务器支持域名及IP地址访问。icp_port3130 3130 缓存服务器之间的通讯协议为ICP,通讯端口为3130,设置缓存服务器间互相通信cache_peer Parent no-query originserver no-digest Parent no-query originserver no-digest本地没有缓存对象时,可以到parent服务器获取缓存对象,no-query本地缓存服务器不会向子级服务器提出查询请求. Originserver设置从源服务器获取数据。no-digest不再从内存摘要中查询其他缓存服务器。Cache_peer_ domain .lotour.com指定访问域名,对该域名的访问,都将从所设定的服务器上读取源文件tcp_recv_ bufsize0 bytes 65535 bytes TCP连接接收缓冲区大小,控制服务器的连接数。client_persistent_connectionsON OFF 不保持客户端与缓存服务器链路层的长时间链接,节省服务器资源。server_persistent_connectionsON ON 保持缓存服务器与源服务器链路层的长时间链接,方便缓存服务器读取数据。half_closed_ clientsON OFF 为安全和节省资源,关闭客户端的半连接cache_ mem8M 1G 设置缓存内存大小为物理内存大小的25%,根据缓存服务器的配置设置为1G,设置过大容易造成服务器当机。cache_ swap_low90% 90% 当缓存内容低于在此设定的数&#20540;时,文件直接添加到缓存中,而不是取代缓存中的旧文件。cache_ swap_high95% 95% 当缓存空间达到设定的数&#20540;后,新内容将取代旧的内容,而不直接添加到缓存中。maximum_ object_ size4096K 4096K 设定磁盘缓存文件的最大&#20540;,大于该数&#20540;的文件不会被缓存minimum_ object_ size0 K 0 K 设定磁盘缓存文件的最小&#20540;,小于该数&#20540;的文件不会被缓存maximum_ object_size_in_ memory8K 4096K 设定内存中缓存单个文件的最大&#20540;,如果大于设定&#20540;,文件不会被缓存。cache_dir100M16*256级子目录Ufs       磁盘文件&#26684;式  /usr/local/squid/var/cache10GUfs/cache 1G的磁盘空间约需要32M内存作内存交换,根据网站现有文件大小,确保能保存3-7天缓存的内容。建议设定10G的磁盘空间作为磁盘缓存,使用16*256级子目录,目录位于/cachecache_ store_log None None缓存服务器存储频繁,记录存储管理的日志庞大,且对管理员无意义,所以我们选择不记录缓存服务器存储管理的活动refresh_ pattern(参数详细解释见备注) reload-into-ims reload-into-ims为没有明确过时期限的响应设置过期规则,使用reload-into-ims 参数时squid在转发请求之前,先发送If-Modified-Since头部到原服务器判断文件是否修改过,根据返回&#20540;确定是否重新加载数据。forward_ timeout4 minutes 20 seconds 设置squid多长时间为请求寻找转发的路径,超过设定时间放弃转发.connect_ timeout1 minute 15seconds 设置squid多长时间等待TCP连接到请求服务器或者邻居服务器,时间超过此&#20540;Squid试图寻找转发请求的路径read_ timeout15 minutes 3minutes 应用于服务器端连接,每次成功读之后,timeout就设置为这个&#20540;.如果这个设置时间之后没有数据再被读,请求就中止.request_ timeout5 minutes 1minutes 初始化连接建立后,多长时间去等待HTTP请求persistent_ request_ timeout2 minutes 15seconds 先前的请求完成后,在一个持续的连接中多长时间去等待下一个HTTP请求client_ lifetime1 day 15minutes 客户连接到缓存服务器后,缓存服务器保留该进程的最大时间,从第一次链接开始计时。shutdown_ lifetime30 seconds 5seconds 关闭squid时,缓存程序处于关闭等待状态,当接收到关闭命令后,在设定的时间过后向用户返回超时信息。Negative_tll 10minutes 设置网页错误信息在缓存中的保留时间。emulate_ httpd_log On 设置日志&#26684;式,apache日志&#26684;式一致.http_access Deny all allow all允许没有被明确禁止的客户进行访问 五、   缓存服务器监控配置实时监控程序主要为三部分:第一部分为流量统计,对当天日志进行分析,获得用户访问的流量统计图表以web方式输出。第二部分为流量监控,主要监控机器的性能,主要包括cpu,mem,swap,disk等。第三部分为squid实时监控.监控squid运行的状态信息,包括如当前内存缓存信息,磁盘缓存信息,服务请求统计,资源使用,存储目录统计监控,源服务器状态等。1.       环境准备安装包Systat             内存状态统计gd-1.8.4-4         图形支持包gd-devel-1.8.4-4perl-5.6.0-17      perl的语言支持perl-CGImod_perl-1.24_01-3libpng-1.0.12-2     png图形支持库libpng-devel-1.0.12-2gcc-2.96-98         c编译器gcc-g77-2.96-98gcc-c&#43;&#43;-2.96-98zlib-1.1.3-24zlib-devel-1.1.3-24httpd(apache)net-snmp           snmp协议mrtg2.       监控配置2.1.      用awstats建立流量统计,日志分析squid配置的日志为apache的&#26684;式,所以用awstats分析。2.1.1. Awstats编译安装与配置:Ø        下载awstats-6.5-1.tar.gzØ        解压安装Tar zxvf  awstats-6.5-1.tar.gz –C /usr/localCd  !$/awstats-6.5-1/tools./awstats_configure.plØ        修改配置文件vi /etc/awstats/awstats.192.168.6.67.confLogFile=&quot;/usr/local/squid/var/logs/access.log&quot; 配置要统计的日志文件LogType=W要分析的日志&#26684;式与Apache相同DirData=&quot;/var/lib/awstats&quot;设置数据的存储目录Ø        每天生成当日的统计分析02 5 * * *  /usr/local/awstats/tools/awstats_updateall.plØ        配置认证与授权   <Location “/awstats/> AuthType Basic AuthName “admin”      AuthUserFile /usr/local/apache/htdocs/awstats.pwdRequire valid-user</Location>Ø        创建口令文件,并添加用户Cd /usr/local/apache/htdocs/Htpasswd –c awstats.pwd adminChown apache.apache swstats.pwdØ        测试      http://192.168.6.67/awstats/awstats.pl?config=192.168.6.672.2.      建立mrtg监控2.2.1. rpm –ivh mrtg.*.rpm配置SNMP 让他可以配合MRTG工作。Ø        编辑/etc/snmp/snmpd.conf把下面的#号去掉#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #在89行把下面的语句#在62行access notConfigGroup &quot;&quot; any noauth exact systemview none none改为:access notConfigGroup &quot;&quot; any noauth exact mib2 none none改完重启一下snmpService snmpd restatØ        生成一个配置文件把配置文件放在apache默认目录下:Cfgmaker –output=/usr/local/apache/htdocs/mrtg/mrtg.cfg squid@192.168.6.67修改配置文件,添加对/opt/mrtg目录下cpu.sh  df.pl  diskio.sh  diskperf.sh  mem.sh   space.sh  swap.sh等脚本的调用以收集信息。Ø        修改配置文件Vi /usr/local/apache/htdocs/mrtg/mrtg.cfgWorkDir: /usr/local/apache/htdocs/mrtg设置mrtg的主目录Options[_]: growright, bits把注释去掉Ø        生成主页,把生成的主页放到指定的mrtg主目录下。/usr/bin/indexmaker –output=/usr/local/apache/htdocs/mrtg/index.html –titile=squid.lotour.com  /usr/local/apache/htdocs/mrtg/mrtg.cfgØ        在/opt/下建立mrtg目录,分别建立程序cpu.sh  df.pl  diskio.sh  diskperf.sh  mem.sh   space.sh  swap.sh,让mrtg配置文件每5分钟调用一次所有这些脚本以收集系统运行的状态信息。(详细代码见附脚本文件)Ø        手工建立一个配置文件,让所有用户可以访问vi /usr/local/apache/conf/mrtg.cfgAlias /mrtg /usr/local/apache/htdocs/mrtg<Location /mrtg>          Order deny,allow         Allow from all    </Location>Ø        配置认证与授权   <Location “/mrtg/> AuthType Basic AuthName “admin”      AuthUserFile /usr/local/apache/htdocs/mrtg.pwdRequire valid-user</Location>Ø        创建口令文件,并添加用户Cd /usr/local/apache/htdocs/Htpasswd –c mrtg.pwd adminChown apache.apache mrtg.pwdØ        设置计划任务每天收集日志分析    */5  *  *  *  *  env LANG=C /usr/bin/mrtg /usr/local/apache/htdocs/mrtg/mrtg.cfg   每五分钟收集一次状态信息。02 4 * * * /usr/bin/indexmaker --output=/usr/local/apache/htdocs/mrtg/index.html --title=lotour67.lotour.com /usr/local/apache/htdocs/mrtg/mrtg.cfgØ        测试   访问: http://192.168.6.67/mrtg/ 2.3.      实时监控squidØ        将脚本复制到web站点的CGI目录Cp /usr/local/squid/cachemgr.cgi /usr/local/apache/htdocs/cgi-binØ        配置对cgi的支持Vi /usr/local/apache/conf/httpd.confScriptAlias /cgi-bin/ “/usr/local/apache/htdocs/cgi-bin”<Directory “/usr/local/apache/htdocs/cgi-bin”>AllowOverride NoneOption NoneOrder allow,denyAllow from all</Directory>AddHandler cgi-script .cgi .plChmod 755 /usr/local/apache/htdocs/cgi-bin/cachemgr.cgiØ        配置授权认证,只有管理员可以查看<Location “/cgi-bin/cachemgr.cgi> AuthType Basic AuthName “admin” AuthUserFile /usr/local/squid/etc/squid.pwd Require valid-user</Location>Ø        创建口令文件,并添加用户Cd /usr/local/squid/etc/Htpasswd –c squid.pwd adminChown apache.apache squid.pwdØ        测试/usr/local/apache/bin/apachectl –k restart在客户端的浏览器中:http://192.168.6.67:8080/cgi-bin/cachemgr.cgi主要实时监控如下参数:MemoryUtilization内存使用监控IPCache Stats and Contents IP缓存统计(IP缓存的个数,命中率等)GeneralRuntime Information 缓存运行状态监控5Minute Average of Counters平均5分钟的缓存访问统计60Minute Average of Counters平均60分钟的缓存访问统计、CacheUtilization缓存使用监控PeerCache Statistics 源服务器统计StoreDirectory Stats存储目录和文件使用监控RequestForwarding Statistics请求转发统计监控以及其它方面的参数等。把以上参数采集统计,分析。2.4.      测试访问http://www.lotour.com点击尽可能所有的网页,注意:1、400,403,500等错误情况。      2、内存池状态,确定磁盘使用的极限周期。      3、是否发现磁盘收发队列及其情况3.     附脚本文件[iyunv@lotour67 mrtg]# cat cpu.sh#!/bin/bashcpuusr=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $3}'`cpusys=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $5}'`UPtime=`/usr/bin/uptime |awk '{print $3&quot;&quot;$4&quot;&quot;$5}'`echo $cpuusrecho $cpusysecho $UPtimehostname[iyunv@lotour67 mrtg]# cat df.pl#!/usr/bin/perlforeach $filesystem (`df -kl | grep -v &quot;Filesystem&quot;`){  @df = split(/\s&#43;/,$filesystem);  $total &#43;= $df[1];  $usage &#43;= $df[2];}print &quot;$total\n&quot;;print &quot;$usage\n&quot;;hostname[iyunv@lotour67 mrtg]# cat diskperf.sh#!/bin/bashhd=hdadisk=/dev/$hdKBread_sec=`iostat -x $disk|grep $hd |awk '{print $8}'`KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print $9}'`echo &quot;$KBread_sec&quot;echo &quot;$KBwrite_sec&quot;hostname[iyunv@lotour67 mrtg]# cat mem.sh#!/bin/bash# This script to monitor the mem usage.totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`echo &quot;$totalmem&quot;echo &quot;$usedmem&quot;[iyunv@lotour67 mrtg]# cat swap.sh#!/bin/bash# This script to monitor the swap usage.totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'`usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'`echo &quot;$totalswap&quot;echo &quot;$usedswap&quot;    六、   备注:1.  refresh_pattern参数解释refresh_pattern的作用:
用于确定一个页面进入cache后,它在cache中停留的时间。

语法:
refresh_pattern [-i] regexp min percent max [options]

几个概念:
resource age =对象进入cache的时间-对象的last_modified
response age  =当前时间-对象进入cache的时间
LM-factor=(response age)/(resource age)

举个例子,这里只考虑percent, 不考虑min  和 max
例如:refresh_pattern   20%
假设源服务器上www.aaa.com/index.htm  ---last-modified2008-01-10 02:00:00
squid上 proxy.aaa.com/index.htm   index.htm进入cache的时间2008-01-10 03:00:00

1)如果当前时间   2008-01-10 03:00:00
resource age =3点-2点=60分钟
response age =0分钟
index.htm还可以在cache停留的时间(resource age)*20%=12分钟
也就是说,index.htm进入cache后,可以停留12分钟,才被重新确认。

2)如果当前时间  2008-01-10 03:05:00
resource age =3点-2点=60分钟
response age =5分钟index.htm还可以在cache中停留的时间(resource age)*20%=12 -5 =7分钟 LM-factor=5/60=8.3%<20%    一直到2008-01-10 03:12:00   LM-factor=12/60=20% 之后,cache中的页面index.htm终于stale。
    如果这时没有index.htm的请求,index.htm会一直在缓存中,如果有index.htm请求,squid收到该请求后,由于已经过期, squid会向源服务器发一个index.htm是否有改变的请求,源服务器收到后,如果index.htm没有更新,squid就不用更新缓存,直接把 缓存的内容放回给客户端,同时,重置对象进入cache的时间为与源服务器确认的时间,比如2008-01-10 03:13:00,如果正好在这个后重新确认了页面。重置后,resource age变长,相应在cache中存活的时间也变长。

   如果有改变则把最新的index.htm返回给squid,squid收到会更新缓存,然后把新的index.htm返回给客户端,同时根据新页面中的Last_Modified和取页面的时间,重新计算resource age,进一步计算出存活时间。

   实际上,一个页面进入cache后,他的存活时间就确定了,即 (resource age) * 百分比,一直到被重新确认。

   squid收到一个页面请求时:
1、计算出response age,
2、如果response age<min 则 fresh  如果response age>max 则 stale
3、如果response age在之间,如果response时间<存活时间,fresh,否则stale      2.     计划任务列表#crontab –e#mrtg每五分钟收集监控的数据*/5 *  *   *   *   env LANG=C /usr/bin/mrtg usr/local/apache/htdocs/mrtg/mrtg.cfg#每天凌晨4点与中国标准时间池进行时间同步0 4 * * * /usr/sbin/ntpdate 210.72.145.44#每天12:01分进行第一次日志轮询,并运行日志拷贝脚本处理.01 12   *  *  *  /usr/local/squid/sbin/squid -k rotate20 12   *  *   *  /bin/bash  /cachelog/chlog.sh#每天18:01分进行第二次日志轮询,并运行日志拷贝脚本和屏幕共享日志处理01 18   * * * /usr/local/squid/sbin/squid -k rotate20 18  *  *  *  /bin/bash  /cachelog/chlog1.sh22 18  *  *  *  /bin/bash  /cachelog/clear.sh#mrtg每天23:58进行更新首页.58  23  *   *   *   /usr/bin/indexmaker --output=/usr/local/apache/htdocs/mrtg/index.html    --title=&quot;Cache2 Monitor&quot; /usr/local/apache/htdocs/mrtg/mrtg.cfg#每天凌晨00:01进行第三次日志轮询,并运行日志拷贝脚本01  0   * * * /usr/local/squid/sbin/squid -k rotate20  0   *  *  *  /bin/bash  /cachelog/chlog2.sh #每天01:01开始分别进行日志分割分步处理,sortlogX.sh完成轮询的三个日志按站点排序.01  1  *  *  * /bin/bash  /cachelog/sortlog.sh02  1  *  *  * /bin/bash  /cachelog/sortlog1.sh03  1  *  *  * /bin/bash  /cachelog/sortlog2.sh#日志排序后,在2:01进行获取各站点在日志文件中的记录数01  2  *  *  *  /bin/bash  /cachelog/get_record_number.sh02  2  *  *  *  /bin/bash  /cachelog/get_record_number1.sh03  2  *  *  *  /bin/bash  /cachelog/get_record_number2.sh#根据上一步各站点获得的记录数,stat_number.sh生成各站点记录范围08  2  *  *  *  /bin/bash  /cachelog/stat_number.sh09  2  *  *  *  /bin/bash  /cachelog/stat_number1.sh10  2  *  *  *  /bin/bash  /cachelog/stat_number2.sh#对上一步进行优化,读取统计站点,去掉不需要统计的站点.12  2  *  *  *  /bin/bash  /cachelog/stat.sh13  2  *  *  *  /bin/bash  /cachelog/stat1.sh14  2  *  *  *  /bin/bash  /cachelog/stat2.sh#根据最后生成的各站点在日志文件中的记录范围,实现日志分割.21  2  *  *  *  /bin/bash  /cachelog/get.sh22  2  *  *  *  /bin/bash  /cachelog/get1.sh23  2  *  *  *  /bin/bash  /cachelog/get2.sh#每天生成昨天总的日志文件30  4  *  *  *  /bin/bash   /cachelog/entirelog/cpentirelog.sh

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141069-1-1.html 上篇帖子: squid 在80端口无法启动 下篇帖子: cacti监控squid的方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表