1.根据访问IP统计UV
awk '{print $1}' access.log|sort | uniq -c |wc -l
2.统计访问URL统计PV
awk '{print $7}' access.log|wc -l
3.查询访问最频繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
4.查询访问最频繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
5.根据时间段统计查看日志
cat access.log| sed -n '/14\/Mar\/2015:21/,/14\/Mar\/2015:22/p'|more
6.zabbix监控
zabbix 配置文件添加
vim /etc/zabbix/zabbix_server.conf
- <span style="color:#656e6a;">### Option: AlertScriptsPath
- # Full path to location of custom alert scripts.
- # Default depends on compilation options.
- #
- # Mandatory: no
- # Default:
- # AlertScriptsPath=${datadir}/zabbix/alertscripts
- </span><span style="color:#ff0000;">AlertScriptsPath=/usr/local/zabbix/sbin</span>
vim /usr/local/zabbix/sbin/nginx-status.py
- import urllib2,base64,sys,getopt,re
- def Usage():
- print "Usage: Nginx-Status.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|requests|reading|writting|waiting]"
- sys.exit(2)
- def main():
- if len(sys.argv) < 6:
- Usage()
- try:
- opts,args = getopt.getopt(sys.argv[1:], "h:p:a:")
- Dict = dict(opts)
- except getopt.GetoptError:
- Usage()
- Nginx_url = "http://" + Dict['-h'] + ":" + Dict['-p'] + "/nginx_status"
- Nginx_req = urllib2.Request(Nginx_url)
- Nginx_res = urllib2.urlopen(Nginx_req)
- Output_key = re.findall(r'\d{1,8}', Nginx_res.read())
- if ( Dict['-a'] == "active"):
- print Output_key[0]
- elif ( Dict['-a'] == "accepted"):
- print Output_key[1]
- elif ( Dict['-a'] == "handled"):
- print Output_key[2]
- elif ( Dict['-a'] == "requests"):
- print Output_key[3]
- elif ( Dict['-a'] == "reading"):
- print Output_key[4]
- elif ( Dict['-a'] == "writting"):
- print Output_key[5]
- elif ( Dict['-a'] == "waiting"):
- print Output_key[6]
- else:
- print "unknown!!"
- sys.exit(1)
- if __name__ == '__main__':
- main()
nginx_status_templates.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <zabbix_export>
- <version>2.0</version>
- <date>2015-11-03T01:37:04Z</date>
- <groups>
- <group>
- <name>nginx-status</name>
- </group>
- </groups>
- <templates>
- <template>
- <template>nginx-status</template>
- <name>nginx-status</name>
- <description/>
- <groups>
- <group>
- <name>nginx-status</name>
- </group>
- </groups>
- <applications/>
- <items>
- <item>
- <name>nginx-accepted</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","accepted"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-active</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","active"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-handled</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","handled"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-reading</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","reading"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-requests</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","requests"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-waiting</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","waiting"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- <item>
- <name>nginx-writting</name>
- <type>10</type>
- <snmp_community/>
- <multiplier>0</multiplier>
- <snmp_oid/>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","writting"]</key>
- <delay>30</delay>
- <history>90</history>
- <trends>365</trends>
- <status>0</status>
- <value_type>3</value_type>
- <allowed_hosts/>
- <units/>
- <delta>0</delta>
- <snmpv3_contextname/>
- <snmpv3_securityname/>
- <snmpv3_securitylevel>0</snmpv3_securitylevel>
- <snmpv3_authprotocol>0</snmpv3_authprotocol>
- <snmpv3_authpassphrase/>
- <snmpv3_privprotocol>0</snmpv3_privprotocol>
- <snmpv3_privpassphrase/>
- <formula>1</formula>
- <delay_flex/>
- <params/>
- <ipmi_sensor/>
- <data_type>0</data_type>
- <authtype>0</authtype>
- <username/>
- <password/>
- <publickey/>
- <privatekey/>
- <port/>
- <description/>
- <inventory_link>0</inventory_link>
- <applications/>
- <valuemap/>
- <logtimefmt/>
- </item>
- </items>
- <discovery_rules/>
- <macros/>
- <templates/>
- <screens/>
- </template>
- </templates>
- <graphs>
- <graph>
- <name>nginx-clients-status</name>
- <width>900</width>
- <height>200</height>
- <yaxismin>0.0000</yaxismin>
- <yaxismax>100.0000</yaxismax>
- <show_work_period>1</show_work_period>
- <show_triggers>1</show_triggers>
- <type>0</type>
- <show_legend>1</show_legend>
- <show_3d>0</show_3d>
- <percent_left>0.0000</percent_left>
- <percent_right>0.0000</percent_right>
- <ymin_type_1>0</ymin_type_1>
- <ymax_type_1>0</ymax_type_1>
- <ymin_item_1>0</ymin_item_1>
- <ymax_item_1>0</ymax_item_1>
- <graph_items>
- <graph_item>
- <sortorder>0</sortorder>
- <drawtype>0</drawtype>
- <color>00C800</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","active"]</key>
- </item>
- </graph_item>
- <graph_item>
- <sortorder>1</sortorder>
- <drawtype>0</drawtype>
- <color>C80000</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","reading"]</key>
- </item>
- </graph_item>
- <graph_item>
- <sortorder>2</sortorder>
- <drawtype>0</drawtype>
- <color>0000C8</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","waiting"]</key>
- </item>
- </graph_item>
- <graph_item>
- <sortorder>3</sortorder>
- <drawtype>0</drawtype>
- <color>C800C8</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","writting"]</key>
- </item>
- </graph_item>
- </graph_items>
- </graph>
- <graph>
- <name>nginx-socket-status</name>
- <width>900</width>
- <height>200</height>
- <yaxismin>0.0000</yaxismin>
- <yaxismax>100.0000</yaxismax>
- <show_work_period>1</show_work_period>
- <show_triggers>1</show_triggers>
- <type>0</type>
- <show_legend>1</show_legend>
- <show_3d>0</show_3d>
- <percent_left>0.0000</percent_left>
- <percent_right>0.0000</percent_right>
- <ymin_type_1>0</ymin_type_1>
- <ymax_type_1>0</ymax_type_1>
- <ymin_item_1>0</ymin_item_1>
- <ymax_item_1>0</ymax_item_1>
- <graph_items>
- <graph_item>
- <sortorder>0</sortorder>
- <drawtype>0</drawtype>
- <color>00C800</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","handled"]</key>
- </item>
- </graph_item>
- <graph_item>
- <sortorder>1</sortorder>
- <drawtype>0</drawtype>
- <color>0000C8</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","requests"]</key>
- </item>
- </graph_item>
- <graph_item>
- <sortorder>2</sortorder>
- <drawtype>0</drawtype>
- <color>C800C8</color>
- <yaxisside>0</yaxisside>
- <calc_fnc>2</calc_fnc>
- <type>0</type>
- <item>
- <host>nginx-status</host>
- <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","accepted"]</key>
- </item>
- </graph_item>
- </graph_items>
- </graph>
- </graphs>
- </zabbix_export>