squid

一、确保Linux 系统中装有NET-SNMP 协议

1.Net-snmp 获取与安装

NET-SNMP官方网站:[url]http://www.net-snmp.org/[/url]

# tar zxvf net-snmp-5.2.4.tar.gz

# cd net-snmp-5.2.4

# ./configure –prefix=/usr/local/net-snmp --enable-developer --with-mib-modules="host agentx examples/example"

# make

# make install

 

# ln –s /usr/local/net-snmp/bin/* /usr/local/bin/

或使用环境变量:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/net-snmp/bin:/usr/local/net-snmp/sbin"

 

# cp EXAMPLE.conf  /usr/local/net-snmp/share/snmp/snmpd.conf

2.Net-snmp 的配置

# vi /etc/rc.d/rc.local

//在rc.local上加入一行/usr/local/net-snmp/sbin/snmpd &,系统启动时启动SNMP服务。

 

修改snmpd.conf 文件,community 为你自定义的snmp字符串,source为允许访问的网络来源,必须指定,否则无法被监控

 

vi /usr/local/net-snmp/share/snmp/snmpd.conf

把这句

com2sec local     localhost       COMMUNITY

com2sec mynetwork NETWORK/24      COMMUNITY

改为

com2sec local     localhost      public

com2sec mynetwork 192.168.1.0/24      public

 

//192.168.1.0为snmp这台机器所以的网段(注意:不充许为192.168.1.1)

3.Net-snmp 常用命令

# /usr/local/net-snmp/sbin/snmpd     //启动SNMP服务

 

##########################################################

用如下命令测试:

snmpget -v 1 -c public localhost system.sysUpTime.0

snmpwalk -v 1 -c public localhost system

查看日志:

tail -f /var/log/snmpd.log

 

#########################################################

控制台显示日志

 

snmptrapd -f -Le -d -M /usr/local/net-snmp/share/snmp/mibs -m ALL

 

#########################################################

1.启动snmpd服务

   #/usr/local/sbin/snmpd -d

 

   //snmptrapd的启动类似

 

2.重启snmpd服务

 

  #/etc/rc.d/init.d/snmpd restart

 

3. 系统启动时启动SNMP服务

  #vi /etc/rc.d/rc.local

   //在rc.local上加入一行/usr/local/net-snmp/sbin/snmpd,系统启动时启动SNMP服务。

 

4.重启snmpd服务

  #cd /etc/rc.d/init.d

  #snmpd restart

 

或#service snmpd restart

二、Squid

1.检查linux是否存在squid老版本

#  rpm -qa|grep squid

#  rpm -e squid  //删除squid老版本

2.Squid 获取与安装

   wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE18.tar.gz

   #  tar -zxvf squid-3.0.STABLE18.tar.gz

   #  cd squid-3.0.STABLE18

   # ./configure --prefix=/usr/local/squid --sysconfdir=/usr/local/squid/etc/ --bindir=/usr/local/squid/bin --sbindir=/usr/local/squid/sbin --mandir=/usr/local/squid/share/man --enable-gnuregex --enable-carp --enable-async-io=80 --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-snmp --enable-arp-acl --enable-htcp --enable-cache-digests --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese" --enable-poll --enable-linux-netfilter --disable-ident-lookups --enable-underscores --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-external-acl-helpers="ip_user" --enable-x-accelerator-vary

   #  make

   #  make install

#  cd ./helpers/basic_auth/NCSA/

#  make

#  cp ncsa_auth /usr/sbin/

#  htpasswd -c /usr/local/squid/password <用户名>

 

./configure --prefix=/usr/local/squid \           #指定squid安装目录

--mandir=/usr/local/squid/share/man \                        #指定man的安装目录

--enable-async-io=20 \                               #采用同步io提高性能,负载高可设大点

--disable-icmp \                                  #禁止icmp协议代理(默认不允许)

--enable-delay-pools \                                #允许延迟限止带宽

--enable-cache-digests \                       #允许缓存摘要,可以加快请求缓冲内容的速度(集群用)

--enable-snmp \                                         #允许snmp协议支持(如用mrtg进行流量监控等)

--disable-ident-lookups \                            #禁止使用RFC931识别

--enable-epoll \                                  #支持epoll的IO模式,2.6以上内核才具有

--enable-basic-auth-helpers="NCSA"           #允许带密码验证,NCSA 风格的用户名和密码档

--enable-linux-netfilter                        #允许透明代理

--enable-arp-acl                                  #允许基于MAC地址的存取过滤

--enable-err-language="Simplify_Chinese"         #支持的错误语言

--enable-default-err-language="Simplify_Chinese"     #指定默认的错误语言

3.Squid 配置

# mv -f /usr/local/squid/etc/squid.conf /usr/local/squid/etc/squid.conf.bak

# vi /usr/local/squid/etc/squid.conf

 

http_port 192.168.253.8:3128 transparent

icp_port  3130

 

cache_dir aufs /usr/local/squid/var/cache 25000 16 256

 

cache_access_log /usr/local/squid/var/logs/access.log

cache_log /usr/local/squid/var/logs/cache.log

cache_store_log /usr/local/squid/var/logs/store.log

pid_filename /usr/local/squid/var/logs/squid.pid

 

 

http_access deny all

http_access allow all

 

cache_effective_user   nobody

cache_effective_group  nobody

cache_mgr x_liyajun@aspire-tech.com

 

acl SSL_ports port 443

acl Safe_ports port 80          # http

acl Safe_ports port 21          # ftp

acl Safe_ports port 443         # https

acl Safe_ports port 70          # gopher

acl Safe_ports port 210         # wais

acl Safe_ports port 1025-65535  # unregistered ports

acl Safe_ports port 280         # http-mgmt

acl Safe_ports port 488         # gss-http

acl Safe_ports port 591         # filemaker

acl Safe_ports port 777         # multiling http

acl Safe_ports port 161         # multiling http

acl CONNECT method CONNECT

http_access allow all

 

##########################################################

#acl snmpManager 192.168.253.8/255.255.255.255

 

acl localhost src 127.0.0.1/32

 

snmp_port 3401

acl snmppublic snmp_community public

snmp_access allow snmppublic localhost

 

 

# mkdir -p /usr/local/squid/var/cache

# chown -R nobody:nobody /usr/local/squid/var

 

#初始化你在 squid.conf 里配置的 cache 目录

# cd /usr/local/squid/var

# squid –z

 

各种启动方式如下:

#squid -k parse        #检查配置是否有错误,没有任何输出,证明配置基本是正确的!!!

 

#squid -zX           #可以加一个X参数,看看初始化具体的项目. 

 

#squid –D           #如果因为DNS无法启动squid,可以用下面的命令启动squid

 

# squid -NCd1       #如果运行正常,终端会显示“Ready to serve requests”

 

另一份squid.config

acl Safe_ports port 21      # ftp

acl Safe_ports port 443     # https

acl Safe_ports port 70      # gopher

acl Safe_ports port 210     # wais

acl Safe_ports port 1025-65535  # unregistered ports

acl Safe_ports port 280     # http-mgmt

acl Safe_ports port 488     # gss-http

acl Safe_ports port 591     # filemaker

acl Safe_ports port 777     # multiling http

acl CONNECT method CONNECT

 

auth_param basic program /usr/sbin/ncsa_auth /usr/local/squid/password

acl normal proxy_auth REQUIRED

http_access allow normal

 

# Deny requests to certain unsafe ports

http_access deny !Safe_ports

 

# Deny CONNECT to other than secure SSL ports

http_access deny CONNECT !SSL_ports

 

# And finally deny all other access to this proxy

http_access deny all

 

# Squid normally listens to port 3128

http_port 3128

 

# We recommend you to use at least the following line.

hierarchy_stoplist cgi-bin ?

 

# Uncomment and adjust the following to add a disk cache directory.

#cache_dir null /tmp

 

# Leave coredumps in the first cache dir

coredump_dir /usr/local/squid/var/cache

 

# Add any of your own refresh_pattern entries above these.

refresh_pattern ^ftp:       1440    20% 10080

refresh_pattern ^gopher:    1440    0%  1440

refresh_pattern -i (/cgi-bin/|\?) 0 0%  0

refresh_pattern .       0   20% 4320

4.Squid 测试

用下面的命令测试:

#  snmpwalk -v1 -c public 127.0.0.1:3401 .1.3.6.1.4.1.3495.1

 

如果能看到类似下面的信息,说明成功了。

SNMPv2-SMI::enterprises.3495.1.1.1.0 = INTEGER: 16360

SNMPv2-SMI::enterprises.3495.1.1.2.0 = INTEGER: 21626872

 

 

 

有因为默认snmp是使用161端口的,而squid的snmp开启的是3401端口,如果想直接通过snmp端口就可以取到squid的相关信息,可以在snmpd.conf中加入

 

view systemview included .1.3.6.1.4.1.3495.1

 

proxy -v 1 -c public localhost:3401 .1.3.6.1.4.1.3495.1

 

用下面的命令测试:

snmpwalk -v1 -c public 127.0.0.1 .1.3.6.1.4.1.3495.1

 

如果能看到类似下面的信息,说明成功了。

SNMPv2-SMI::enterprises.3495.1.1.1.0 = INTEGER: 16360

SNMPv2-SMI::enterprises.3495.1.1.2.0 = INTEGER: 21626872

 

5.Squid 常用命令

1,对你的squid.conf 排错,即验证 squid.conf 的语法和配置。

 

#squid -k parse

 

如果squid.conf 有语法或配置错误,这里会返回提示你,如果没有返回,恭喜,可以尝试启动squid。

 

2,在前台启动squid,并输出启动过程。

 

#squid -N -d1

 

如果有到 ready to server reques,恭喜,启动成功。

然后 ctrl + c,停止squid,并以后台运行的方式启动它。

3,启动squid在后台运行。

 

#squid -s

 

这时候可以 ps -A 来查看系统进程,可以看到俩个 squid 进程。

4,停止 squid

 

#squid -k shutdown

 

这个不用解释吧。

5,重引导修改过的 squid.conf

 

#squid -k reconfigure

 

6.Squid 其他

1,把squid添加到系统启动项

编辑 /etc/rc.d/rc.local

添加如下行:  /usr/local/squid/sbin/squid -s

当然,并不是每个人都喜欢这种启动方式,你可以用你最习惯的方式;或者把它安装为服务。

 

2,修改cache 缓存目录的权限。

#chown -R nobody:nobody /home/cache

我的cache缓存目录是 /home/cache, nobody执行用户和用户组是nobody,nobody。

3,修改squid 日志目录的权限

#chown -R squid:squid /usr/local/squid/var/logs

这一步并不是适合每一个使用squid的用户.意为让squid有权限在该目录进行写操作。

例如生成  access.log    cache.log   store.log

 

4,查看你的日志文档。

#more /usr/local/squid/var/logs/access.log | grep TCP_MEM_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到内存中,并返回给访问用户。
#more /usr/local/squid/var/logs/access.log | grep TCP_HIT
该指令可以看到在squid运行过程中,有那些文件被squid缓存到cache目录中,并返回给访问用户。
#more /usr/local/squid/var/logs/access.log | grep TCP_MISS
该指令可以看到在squid运行过程中,有那些文件没有被squid缓存,而是现重原始服务器获取并返回给访问用户。

 

7.Squid.conf 配置实例

8.Squid.conf 配置说明

lSquid配置(仅对必要选项配置并说明):

http_port 80

此选项用于Squid代理所侦听的端口,由于要实现Http加速代理,则侦听端口80

tcp_outgoing_address 255.255.255.255

此句法指定了远程服务器的IP地址,这里我们使用255.255.255.255来指定为所有地址

tcp_incoming_address

这里可以使用http_port语法进行绑定端口和IP地址。表示客户端的请求。可以不使用。

cache_mem 64 MB

由于Squid在读写磁盘,I/O通道,数据转贮等需要大量内存,我们可以把内存值稍微设置大一些,这样可以提供服务器性能。

 

cache_swap_low 95

这用做表示缓存内部对象的替换的衡量基线。当交换分区的磁盘利用率超越这个基线,那么缓存对象替换也就随之开始。这里是用百分率做衡量标准。

 

cache_swap_high 98

此标记用于缓存对象替换的最高程度,当交换分区磁盘利用率接近此设置,则表示对象的更新程度更为剧烈。这里由于我的缓存区设置比较大,因此设置为98%。

 

下面几个配置标记采用系统默认配置。

{ maximum_object_size 4096 KB

{ 超过此尺寸的对象将不缓存。

{ minimum_object_size 0 KB

{ 小于此尺寸的对象将不缓存。

{ maximum_object_size_in_memory 8 KB

{ 内存中能缓存的最大对象尺寸。

{ ipcache_size 1024

{ 指定IP缓存大小。

{ ipcache_low 90

{ 缓存IP地址的最低基线。

{ ipcache_high 95

{ 缓存IP地址的最高极限。

{ fqdncache_size 1024

{ 缓存DNS全域名解析的尺寸。

 

cache_replacement_policy lru

此标记用于当缓存新对象时,使用缓存策略来清除缓存中特定对象。这里使用lru表示:它只替换长时间没有被访问过的对象。其他策略请参看配置文档介绍。

 

memory_replacement_policy lru

此用法同上,区别在于替换内存对象。

 

cache_dir ufs /cache 4000 16 256

设置缓存根目录为/cache,类型为ufs,缓存区大小为4G,可以有16个二级子目录,每有二级目录有256个三级子目录。

 

cache_access_log /var/log/squid/access.log

cache_log /var/log/squid/cache.log

cache_store_log /var/log/squid/store.log

cache_swap_log /var/log/squid/swap.log

上面几项是缓存日志记录的路径和文件名。

 

log_ip_on_direct on

记录客户端主机的IP地址。

 

log_fqdn on

记录全DNS域名解析。

 

mime_table /usr/local/squid/etc/mime.conf

Squid所用mime的文件路径

 

pid_filename /usr/local/squid/logs/squid.pid

Squid进程ID的文件。

 

ftp_user Squid@ynst.net.cn

这里可以使用匿名登陆FTP服务器。

 

ftp_list_width 32

FTP文件列表长度,超过长度则截断文件名。

ftp_passive on

允许主动连接FTP服务器。

 

dns_nameservers 202.98.187.1

指定代理的域名解析服务器。

 

authenticate_program  /usr/local/squid/bin/ncsa_auth

/usr/local/squid/etc/passwd

代理认证程序,这里在/usr/local/squid/etc使用htpasswd产生一个密码文件,具体方法如下:

#htpasswd –cdb passwd auth_usr1 pass1

#htpasswd –db passwd auth_usr2 pass2

#htpasswd –db passwd auth_usr3 pass3

创建passwd文件,并添加auth_usr1,auth_usr2,auth_usr3三个认证用户。

 

authenticate_children 5

所需要要产生的认证进程数。

 

authenticate_ttl 3600

此标记设置认证保持时间。

 

authenticate_ip_ttl 1800

此标记设置认证绑定IP地址的时间长度。

 

authenticate_ip_ttl_is_strict on

此标记可在authenticate_ttl时间内,拒绝其他非认证IP地址访问。

 

request_header_max_size 10 KB

request_body_max_size 1 MB

设置了Http请求的包头和数据大小。

 

request_body_max_size 0

这里,请求所返回的数据大小为0,意义为没有任何限制。

 

connect_timeout 180 seconds

read_timeout 15 minutes

request_timeout 20 seconds

上面几项是代理服务几个超时设置,这里使用配置文件默认值。

 

下面是访问控制列表一些配置,这里对访问的IP地址,域名,访问类型极其访问代理的权限做的限制:

acl acldomain dstdomain .ynst.net.cn

acl acceleratedport port 80

acl acceleratedhost dst 202.98.187.17/255.255.255.255

acl aclprotocol proto HTTP FTP

acl aclmethod method GET POST

 

acl aclauth proxy_auth required 

(注:这里使用required表示任何passwd文件中存在的合法用户才授予访问权限。)

 

acl aclsnmp snmp_community secrect

MRTG可以使用snmp协议检测此代理服务运行状态。

 

acl aclconn maxconn 20

可并行连接的最大数量。

 

启用定义的访问控制列表

http_access allow manager localhost

http_access deny manager

http_access allow aclauth

http_access allow acceleratedport acceleratedhost

http_access deny all

 

下面为缓存管理设置

cache_mgr admin@ynst.net.cn

设置缓存管理者的接受邮件地址。

 

cache_effective_user squidusr

cache_effective_group squidgrp

设置执行Squid的用户和用户组。

{注:设置用户和用户组及其权限修改方法如下:

#groupadd squidgrp

#useradd –g squidgrp –d /home/squidusr squidusr

修改log日志权限属性,使squidusr有写权限。

#chown squidusr /var/log/squid

#chgrp&

 

9.Squid命中率分析

/usr/local/squid/bin/squidclient -p 80 mgr:info
/usr/local/squid/bin/squidclient -p 80 mgr:5min
可以看到详细的性能情况,其中PORT是你的proxy的端口,5min可以是60min

取得squid运行状态信息: squidclient -p 80 mgr:info
*取得squid内存使用情况: squidclient -p 80 mgr:mem
*取得squid已经缓存的列表: squidclient -p 80 mgr:objects                    . use it carefully,it may crash
*取得squid的磁盘使用情况: squidclient -p 80 mgr:diskd
*强制更新某个url:squidclient -p 80 -m PURGE http://www.xxx.com/xxx.php
*更多的请查看:squidclient-h 或者 squidclient -p 80 mgr:

查命中率:
/usr/local/squid/bin/squidclient -h 111.222.111.111 -p80 mgr:info

/usr/local/squid/bin/squidclient -h具体的IP -p80 mgr:info

(我们一般都会把squid配置成有缓存目录的模式,也就是有配置cache_dir目录,当squid应用运行了一段时间以后,cache_dir对应的swap.state文件就会变得越来越大,里面的无效接口数据越来越多,这可能在一定程度上影响squid的响应时间,此时我们可以使用rotate命令来使squid清理swap.state里面的无效数据,减少swap.state的大小。具体命令格式:
/path/to/squid/sbin/squid -k rotate -f /path/to/squid/conf_file
但是在3.0.7以前的版本都发现一些问题,rotate完以后启动DNSSERVER的时候会提示不能分配内存,从而导致DNSSERVER的辅助进程不能启动,此时一般情况下需要重启squid应用。)

10.Squid 排错

 检查缓存目录中的cache.log文件可以判断squid工作是否正常。此文件包含squid运行时产生的输出信息和错误信息。

安装sarg

# mkdir /usr/local/sarg
# tar zxvf sarg-2.2.5.tar.gz
# cd sarg-2.2.5
# ./configure --prefix=/usr/local/sarg --enable-mandir=/usr/local/sarg/man --enable-bindir=/usr/local/sarg/bin
# make
# make install
# vi /usr/local/sarg/sarg.conf
language english             //指定网页报告文件的语言类型,不支持中文网页。
access_log /usr/local/squid/var/logs/access.log //指定squid日志文件绝对路径。
title "squid user access reports"         //指定网页标题
output_dir /usr/local/apache/htdocs/sarg          //指定网页报告文件输出路径
overwrite_report no          //是否覆盖报告,当那个日期的报告已经存在时是否覆盖掉
mail_utility mail
topsites_num 100
exclude_codes /usr/local/sarg/exclude_codes
max_elapsed 28800000
charset UTF-8
exclude_hosts /usr/local/sarg/norecords //指定不计入排序的站点列表文件名
resolve_ip no
user_ip no                          //使用用户名显示
# vi /usr/local/sarg/norecords      //添加不计排序站点
google.com
microsoft.com
baidu.com
# /usr/local/sarg/bin/sarg     //生成报表
# vi/root/sarg-hour.sh //每小时生成
#!/bin/bash
#hour
Date=$(date -d "1 hours ago " +"%y%m%d%H")
/usr/local/squid/sbin/squid -k rotate
sleep 10
Squiddir='/usr/local/squid/var/logs'
Access='access.log.*'
/bin/mv ${Squiddir}${Access} ${Squiddir}access${Date}.log
/usr/local/sarg/bin/sarg -o /usr/local/apache/htdocs/hours -l ${Squiddir}access${Date}.log
#chmod a+x /root/sarg-hour.sh
# crontab -e
0 8,9,10,11,12,13,14,15,16,17,18,19,20 * * * /root/sarg-hour.sh
# vi /root/sarg-day.sh
#!/bin/bash
#day
Squiddir='/usr/local/squid/var/logs'
Date=$(date -d "1 day ago " +"%y%m%d")
/bin/sort -m -t " " -k 1 -o $Squiddir${Date}log_all.log ${Squiddir}access${Date}*.log
/bin/rm access${Date}*.log -rf
/usr/local/sarg/bin/sarg -o /usr/local/apache/htdocs/days -l ${Squiddir}${Date}log_all.log
# chmod a+x /root/sarg-day.sh
# crontab -e
20 1 * * * /root/sarg-day.sh
# vi /root/sarg-week.sh
#!/bin/bash
#week
Date=$(date -d "1 week ago " +"%y%m%d")
YESTERDAY=$(date –date "1 week ago" +%d/%m/%Y)
/bin/sort -m -t " " -k 1 -o $Squiddir${Date}weeklog_all.log ${Squiddir}*log_all.log
/usr/local/sarg/bin/sarg -o /usr/local/apache/week -l ${Squiddir}${Date}weeklog_all.log
# chmod a+x /root/sarg-week.sh
# crontab -e
40 * * * 5 /root/sarg-week.sh
或者        //每十分钟生成一次日志
# crontab -e
*/10 * * * * /usr/local/sarg/bin/sarg > /dev/null 2>&1

相关推荐