Nginx日志访问统计

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

[html]  view plain  copy
  1. <span style="color:#656e6a;">### Option: AlertScriptsPath  
  2. #       Full path to location of custom alert scripts.  
  3. #       Default depends on compilation options.  
  4. #  
  5. # Mandatory: no  
  6. # Default:  
  7. AlertScriptsPath=${datadir}/zabbix/alertscripts  
  8. </span><span style="color:#ff0000;">AlertScriptsPath=/usr/local/zabbix/sbin</span>  

vim /usr/local/zabbix/sbin/nginx-status.py 

[python]  view plain  copy
  1. import urllib2,base64,sys,getopt,re  
  2.   
  3. def Usage():  
  4.     print "Usage: Nginx-Status.py -h 127.0.0.1 -p 80 -a [active|accepted|handled|requests|reading|writting|waiting]"  
  5.     sys.exit(2)  
  6.   
  7. def main():  
  8.     if len(sys.argv) < 6:  
  9.         Usage()  
  10.   
  11.     try:  
  12.         opts,args = getopt.getopt(sys.argv[1:], "h:p:a:")  
  13.         Dict = dict(opts)  
  14.     except getopt.GetoptError:  
  15.         Usage()  
  16.   
  17.     Nginx_url = "http://" + Dict['-h'] + ":" + Dict['-p'] + "/nginx_status"  
  18.     Nginx_req = urllib2.Request(Nginx_url)  
  19.     Nginx_res = urllib2.urlopen(Nginx_req)  
  20.     Output_key = re.findall(r'\d{1,8}', Nginx_res.read())  
  21.   
  22.     if ( Dict['-a'] == "active"):  
  23.             print Output_key[0]  
  24.     elif ( Dict['-a'] == "accepted"):  
  25.             print Output_key[1]  
  26.     elif ( Dict['-a'] == "handled"):  
  27.             print Output_key[2]  
  28.     elif ( Dict['-a'] == "requests"):  
  29.             print Output_key[3]  
  30.     elif ( Dict['-a'] == "reading"):  
  31.             print Output_key[4]  
  32.     elif ( Dict['-a'] == "writting"):  
  33.             print Output_key[5]  
  34.     elif ( Dict['-a'] == "waiting"):  
  35.             print Output_key[6]  
  36.     else:  
  37.             print "unknown!!"  
  38.             sys.exit(1)  
  39.   
  40. if __name__ == '__main__':  
  41.     main()  

nginx_status_templates.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <zabbix_export>  
  3.     <version>2.0</version>  
  4.     <date>2015-11-03T01:37:04Z</date>  
  5.     <groups>  
  6.         <group>  
  7.             <name>nginx-status</name>  
  8.         </group>  
  9.     </groups>  
  10.     <templates>  
  11.         <template>  
  12.             <template>nginx-status</template>  
  13.             <name>nginx-status</name>  
  14.             <description/>  
  15.             <groups>  
  16.                 <group>  
  17.                     <name>nginx-status</name>  
  18.                 </group>  
  19.             </groups>  
  20.             <applications/>  
  21.             <items>  
  22.                 <item>  
  23.                     <name>nginx-accepted</name>  
  24.                     <type>10</type>  
  25.                     <snmp_community/>  
  26.                     <multiplier>0</multiplier>  
  27.                     <snmp_oid/>  
  28.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","accepted"]</key>  
  29.                     <delay>30</delay>  
  30.                     <history>90</history>  
  31.                     <trends>365</trends>  
  32.                     <status>0</status>  
  33.                     <value_type>3</value_type>  
  34.                     <allowed_hosts/>  
  35.                     <units/>  
  36.                     <delta>0</delta>  
  37.                     <snmpv3_contextname/>  
  38.                     <snmpv3_securityname/>  
  39.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  40.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  41.                     <snmpv3_authpassphrase/>  
  42.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  43.                     <snmpv3_privpassphrase/>  
  44.                     <formula>1</formula>  
  45.                     <delay_flex/>  
  46.                     <params/>  
  47.                     <ipmi_sensor/>  
  48.                     <data_type>0</data_type>  
  49.                     <authtype>0</authtype>  
  50.                     <username/>  
  51.                     <password/>  
  52.                     <publickey/>  
  53.                     <privatekey/>  
  54.                     <port/>  
  55.                     <description/>  
  56.                     <inventory_link>0</inventory_link>  
  57.                     <applications/>  
  58.                     <valuemap/>  
  59.                     <logtimefmt/>  
  60.                 </item>  
  61.                 <item>  
  62.                     <name>nginx-active</name>  
  63.                     <type>10</type>  
  64.                     <snmp_community/>  
  65.                     <multiplier>0</multiplier>  
  66.                     <snmp_oid/>  
  67.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","active"]</key>  
  68.                     <delay>30</delay>  
  69.                     <history>90</history>  
  70.                     <trends>365</trends>  
  71.                     <status>0</status>  
  72.                     <value_type>3</value_type>  
  73.                     <allowed_hosts/>  
  74.                     <units/>  
  75.                     <delta>0</delta>  
  76.                     <snmpv3_contextname/>  
  77.                     <snmpv3_securityname/>  
  78.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  79.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  80.                     <snmpv3_authpassphrase/>  
  81.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  82.                     <snmpv3_privpassphrase/>  
  83.                     <formula>1</formula>  
  84.                     <delay_flex/>  
  85.                     <params/>  
  86.                     <ipmi_sensor/>  
  87.                     <data_type>0</data_type>  
  88.                     <authtype>0</authtype>  
  89.                     <username/>  
  90.                     <password/>  
  91.                     <publickey/>  
  92.                     <privatekey/>  
  93.                     <port/>  
  94.                     <description/>  
  95.                     <inventory_link>0</inventory_link>  
  96.                     <applications/>  
  97.                     <valuemap/>  
  98.                     <logtimefmt/>  
  99.                 </item>  
  100.                 <item>  
  101.                     <name>nginx-handled</name>  
  102.                     <type>10</type>  
  103.                     <snmp_community/>  
  104.                     <multiplier>0</multiplier>  
  105.                     <snmp_oid/>  
  106.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","handled"]</key>  
  107.                     <delay>30</delay>  
  108.                     <history>90</history>  
  109.                     <trends>365</trends>  
  110.                     <status>0</status>  
  111.                     <value_type>3</value_type>  
  112.                     <allowed_hosts/>  
  113.                     <units/>  
  114.                     <delta>0</delta>  
  115.                     <snmpv3_contextname/>  
  116.                     <snmpv3_securityname/>  
  117.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  118.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  119.                     <snmpv3_authpassphrase/>  
  120.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  121.                     <snmpv3_privpassphrase/>  
  122.                     <formula>1</formula>  
  123.                     <delay_flex/>  
  124.                     <params/>  
  125.                     <ipmi_sensor/>  
  126.                     <data_type>0</data_type>  
  127.                     <authtype>0</authtype>  
  128.                     <username/>  
  129.                     <password/>  
  130.                     <publickey/>  
  131.                     <privatekey/>  
  132.                     <port/>  
  133.                     <description/>  
  134.                     <inventory_link>0</inventory_link>  
  135.                     <applications/>  
  136.                     <valuemap/>  
  137.                     <logtimefmt/>  
  138.                 </item>  
  139.                 <item>  
  140.                     <name>nginx-reading</name>  
  141.                     <type>10</type>  
  142.                     <snmp_community/>  
  143.                     <multiplier>0</multiplier>  
  144.                     <snmp_oid/>  
  145.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","reading"]</key>  
  146.                     <delay>30</delay>  
  147.                     <history>90</history>  
  148.                     <trends>365</trends>  
  149.                     <status>0</status>  
  150.                     <value_type>3</value_type>  
  151.                     <allowed_hosts/>  
  152.                     <units/>  
  153.                     <delta>0</delta>  
  154.                     <snmpv3_contextname/>  
  155.                     <snmpv3_securityname/>  
  156.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  157.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  158.                     <snmpv3_authpassphrase/>  
  159.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  160.                     <snmpv3_privpassphrase/>  
  161.                     <formula>1</formula>  
  162.                     <delay_flex/>  
  163.                     <params/>  
  164.                     <ipmi_sensor/>  
  165.                     <data_type>0</data_type>  
  166.                     <authtype>0</authtype>  
  167.                     <username/>  
  168.                     <password/>  
  169.                     <publickey/>  
  170.                     <privatekey/>  
  171.                     <port/>  
  172.                     <description/>  
  173.                     <inventory_link>0</inventory_link>  
  174.                     <applications/>  
  175.                     <valuemap/>  
  176.                     <logtimefmt/>  
  177.                 </item>  
  178.                 <item>  
  179.                     <name>nginx-requests</name>  
  180.                     <type>10</type>  
  181.                     <snmp_community/>  
  182.                     <multiplier>0</multiplier>  
  183.                     <snmp_oid/>  
  184.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","requests"]</key>  
  185.                     <delay>30</delay>  
  186.                     <history>90</history>  
  187.                     <trends>365</trends>  
  188.                     <status>0</status>  
  189.                     <value_type>3</value_type>  
  190.                     <allowed_hosts/>  
  191.                     <units/>  
  192.                     <delta>0</delta>  
  193.                     <snmpv3_contextname/>  
  194.                     <snmpv3_securityname/>  
  195.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  196.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  197.                     <snmpv3_authpassphrase/>  
  198.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  199.                     <snmpv3_privpassphrase/>  
  200.                     <formula>1</formula>  
  201.                     <delay_flex/>  
  202.                     <params/>  
  203.                     <ipmi_sensor/>  
  204.                     <data_type>0</data_type>  
  205.                     <authtype>0</authtype>  
  206.                     <username/>  
  207.                     <password/>  
  208.                     <publickey/>  
  209.                     <privatekey/>  
  210.                     <port/>  
  211.                     <description/>  
  212.                     <inventory_link>0</inventory_link>  
  213.                     <applications/>  
  214.                     <valuemap/>  
  215.                     <logtimefmt/>  
  216.                 </item>  
  217.                 <item>  
  218.                     <name>nginx-waiting</name>  
  219.                     <type>10</type>  
  220.                     <snmp_community/>  
  221.                     <multiplier>0</multiplier>  
  222.                     <snmp_oid/>  
  223.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","waiting"]</key>  
  224.                     <delay>30</delay>  
  225.                     <history>90</history>  
  226.                     <trends>365</trends>  
  227.                     <status>0</status>  
  228.                     <value_type>3</value_type>  
  229.                     <allowed_hosts/>  
  230.                     <units/>  
  231.                     <delta>0</delta>  
  232.                     <snmpv3_contextname/>  
  233.                     <snmpv3_securityname/>  
  234.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  235.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  236.                     <snmpv3_authpassphrase/>  
  237.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  238.                     <snmpv3_privpassphrase/>  
  239.                     <formula>1</formula>  
  240.                     <delay_flex/>  
  241.                     <params/>  
  242.                     <ipmi_sensor/>  
  243.                     <data_type>0</data_type>  
  244.                     <authtype>0</authtype>  
  245.                     <username/>  
  246.                     <password/>  
  247.                     <publickey/>  
  248.                     <privatekey/>  
  249.                     <port/>  
  250.                     <description/>  
  251.                     <inventory_link>0</inventory_link>  
  252.                     <applications/>  
  253.                     <valuemap/>  
  254.                     <logtimefmt/>  
  255.                 </item>  
  256.                 <item>  
  257.                     <name>nginx-writting</name>  
  258.                     <type>10</type>  
  259.                     <snmp_community/>  
  260.                     <multiplier>0</multiplier>  
  261.                     <snmp_oid/>  
  262.                     <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","writting"]</key>  
  263.                     <delay>30</delay>  
  264.                     <history>90</history>  
  265.                     <trends>365</trends>  
  266.                     <status>0</status>  
  267.                     <value_type>3</value_type>  
  268.                     <allowed_hosts/>  
  269.                     <units/>  
  270.                     <delta>0</delta>  
  271.                     <snmpv3_contextname/>  
  272.                     <snmpv3_securityname/>  
  273.                     <snmpv3_securitylevel>0</snmpv3_securitylevel>  
  274.                     <snmpv3_authprotocol>0</snmpv3_authprotocol>  
  275.                     <snmpv3_authpassphrase/>  
  276.                     <snmpv3_privprotocol>0</snmpv3_privprotocol>  
  277.                     <snmpv3_privpassphrase/>  
  278.                     <formula>1</formula>  
  279.                     <delay_flex/>  
  280.                     <params/>  
  281.                     <ipmi_sensor/>  
  282.                     <data_type>0</data_type>  
  283.                     <authtype>0</authtype>  
  284.                     <username/>  
  285.                     <password/>  
  286.                     <publickey/>  
  287.                     <privatekey/>  
  288.                     <port/>  
  289.                     <description/>  
  290.                     <inventory_link>0</inventory_link>  
  291.                     <applications/>  
  292.                     <valuemap/>  
  293.                     <logtimefmt/>  
  294.                 </item>  
  295.             </items>  
  296.             <discovery_rules/>  
  297.             <macros/>  
  298.             <templates/>  
  299.             <screens/>  
  300.         </template>  
  301.     </templates>  
  302.     <graphs>  
  303.         <graph>  
  304.             <name>nginx-clients-status</name>  
  305.             <width>900</width>  
  306.             <height>200</height>  
  307.             <yaxismin>0.0000</yaxismin>  
  308.             <yaxismax>100.0000</yaxismax>  
  309.             <show_work_period>1</show_work_period>  
  310.             <show_triggers>1</show_triggers>  
  311.             <type>0</type>  
  312.             <show_legend>1</show_legend>  
  313.             <show_3d>0</show_3d>  
  314.             <percent_left>0.0000</percent_left>  
  315.             <percent_right>0.0000</percent_right>  
  316.             <ymin_type_1>0</ymin_type_1>  
  317.             <ymax_type_1>0</ymax_type_1>  
  318.             <ymin_item_1>0</ymin_item_1>  
  319.             <ymax_item_1>0</ymax_item_1>  
  320.             <graph_items>  
  321.                 <graph_item>  
  322.                     <sortorder>0</sortorder>  
  323.                     <drawtype>0</drawtype>  
  324.                     <color>00C800</color>  
  325.                     <yaxisside>0</yaxisside>  
  326.                     <calc_fnc>2</calc_fnc>  
  327.                     <type>0</type>  
  328.                     <item>  
  329.                         <host>nginx-status</host>  
  330.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","active"]</key>  
  331.                     </item>  
  332.                 </graph_item>  
  333.                 <graph_item>  
  334.                     <sortorder>1</sortorder>  
  335.                     <drawtype>0</drawtype>  
  336.                     <color>C80000</color>  
  337.                     <yaxisside>0</yaxisside>  
  338.                     <calc_fnc>2</calc_fnc>  
  339.                     <type>0</type>  
  340.                     <item>  
  341.                         <host>nginx-status</host>  
  342.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","reading"]</key>  
  343.                     </item>  
  344.                 </graph_item>  
  345.                 <graph_item>  
  346.                     <sortorder>2</sortorder>  
  347.                     <drawtype>0</drawtype>  
  348.                     <color>0000C8</color>  
  349.                     <yaxisside>0</yaxisside>  
  350.                     <calc_fnc>2</calc_fnc>  
  351.                     <type>0</type>  
  352.                     <item>  
  353.                         <host>nginx-status</host>  
  354.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","waiting"]</key>  
  355.                     </item>  
  356.                 </graph_item>  
  357.                 <graph_item>  
  358.                     <sortorder>3</sortorder>  
  359.                     <drawtype>0</drawtype>  
  360.                     <color>C800C8</color>  
  361.                     <yaxisside>0</yaxisside>  
  362.                     <calc_fnc>2</calc_fnc>  
  363.                     <type>0</type>  
  364.                     <item>  
  365.                         <host>nginx-status</host>  
  366.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","writting"]</key>  
  367.                     </item>  
  368.                 </graph_item>  
  369.             </graph_items>  
  370.         </graph>  
  371.         <graph>  
  372.             <name>nginx-socket-status</name>  
  373.             <width>900</width>  
  374.             <height>200</height>  
  375.             <yaxismin>0.0000</yaxismin>  
  376.             <yaxismax>100.0000</yaxismax>  
  377.             <show_work_period>1</show_work_period>  
  378.             <show_triggers>1</show_triggers>  
  379.             <type>0</type>  
  380.             <show_legend>1</show_legend>  
  381.             <show_3d>0</show_3d>  
  382.             <percent_left>0.0000</percent_left>  
  383.             <percent_right>0.0000</percent_right>  
  384.             <ymin_type_1>0</ymin_type_1>  
  385.             <ymax_type_1>0</ymax_type_1>  
  386.             <ymin_item_1>0</ymin_item_1>  
  387.             <ymax_item_1>0</ymax_item_1>  
  388.             <graph_items>  
  389.                 <graph_item>  
  390.                     <sortorder>0</sortorder>  
  391.                     <drawtype>0</drawtype>  
  392.                     <color>00C800</color>  
  393.                     <yaxisside>0</yaxisside>  
  394.                     <calc_fnc>2</calc_fnc>  
  395.                     <type>0</type>  
  396.                     <item>  
  397.                         <host>nginx-status</host>  
  398.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","handled"]</key>  
  399.                     </item>  
  400.                 </graph_item>  
  401.                 <graph_item>  
  402.                     <sortorder>1</sortorder>  
  403.                     <drawtype>0</drawtype>  
  404.                     <color>0000C8</color>  
  405.                     <yaxisside>0</yaxisside>  
  406.                     <calc_fnc>2</calc_fnc>  
  407.                     <type>0</type>  
  408.                     <item>  
  409.                         <host>nginx-status</host>  
  410.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","requests"]</key>  
  411.                     </item>  
  412.                 </graph_item>  
  413.                 <graph_item>  
  414.                     <sortorder>2</sortorder>  
  415.                     <drawtype>0</drawtype>  
  416.                     <color>C800C8</color>  
  417.                     <yaxisside>0</yaxisside>  
  418.                     <calc_fnc>2</calc_fnc>  
  419.                     <type>0</type>  
  420.                     <item>  
  421.                         <host>nginx-status</host>  
  422.                         <key>nginx-status.py["-h","{HOST.IP}","-p","80","-a","accepted"]</key>  
  423.                     </item>  
  424.                 </graph_item>  
  425.             </graph_items>  
  426.         </graph>  
  427.     </graphs>  
  428. </zabbix_export> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值