nginx qos 계산 sql

이전에 일부 nginx qos 분석 과 cdn 로그 qos 분석 컴 퓨 팅 프로그램 개발 을 했 는데 실시 간 으로 오프라인 nginx 로그 에 대한 qos 계산 과 관련된다.연구 개발 이 든 운영 이 든 모두 자신 이 맡 은 domain 의 qos 상황 에 관심 을 가지 고 업무 의 운영 상황, 용량 상황, 확장 이 필요 한 지, 그리고 응용 프로그램의 변경 이 사용자 의 방문 에 영향 을 미 치 는 지 검증 해 야 한다.
 가장 흔히 볼 수 있 는 것 은 nginx code 에 대한 분석 이다. 예 를 들 어 도 메 인 이름 의 방 문 량, 가용성 (2xx + 3xx 의 비율), 4xx, 5xx 의 비율, 평균 응답 시간, 평균 파일 크기, 백 엔 드 응답 시간 (upstream time) 등 이다.
 가끔 은 응용 url 호출 상황 을 분석 해 야 한다. 예 를 들 어 방 문 량 이 가장 많은 url, 속도 가 가장 느 린 url, 오류 율 이 가장 높 은 url, 방문 상황 의 동기 대비 환 비 등 은 특정한 문 제 를 신속하게 찾 을 수 있다.또한 사용자 의 분포, 각 지역 사용자 의 qos 상황 등 더욱 상세 한 데 이 터 를 추가 할 수 있다.
1. 오프라인 qos 계산 은 nginx 로 그 를 hadop 에 가 져 온 다음 에 hive 를 통 해 계산 하 는 것 입 니 다. 예 를 들 어 cdn 로그 에 대한 qos 분석, nginx 소스 에 대한 분석 등 입 니 다.
여기 자주 사용 하 는 sql 몇 개 를 열거 하 였 으 니 참고 하 시기 바 랍 니 다.
예 를 들 어 각 성에 대한 응답 시간 분포
"""select ip_province,round(SUM(IF(response<=10 AND STATUS='200',1,0))*100/COUNT(1),4) as less10ms_ratio,
round(SUM(IF(response<=100 AND STATUS='200',1,0))*100/COUNT(1),4) as
less100ms_ratio,
round(SUM(IF(response<=1000 AND STATUS='200',1,0))*100/COUNT(1),4) as less1000ms_ratio,
round(avg(response)/1000,4) as avg_rt,round(sum(size)*8/(1000*(sum(response)/1000)),2) as svg_speed
FROM %s where dt = 'xxx' and domain = 'xxxx' and status='200' and hour in ('09','10','11')
group by ip_province order by avg_rt desc """
 
   
分析404最多的url,refer
SELECT regexp_extract(request,'(.*?) (.*?) (.*?)',2),http_referer,COUNT(1)
AS COUNT_ALL FROM viplog.dw_nginx_log WHERE dt='xxx' and host='xxx' and
status='404' GROUP BY regexp_extract(request,'(.*?) (.*?) (.*?)',2),
http_referer ORDER BY COUNT_ALL DESC limit 20

도 메 인 이름 의 http 코드 별 비율 에 대한 분석:
"""select a.status as code,a.count_all,round((a.count_all/b.total)*100,4)
as ratio from (select  status,count(1) as count_all from viplog.dw_nginx_log
where dt='xxx' and host='xxx' group by status ) a join
(select count(1) as total from viplog.dw_nginx_log where host='%s'
and dt='xxx') b order by ratio desc """

cdn 제조 업 체 qos 분석:
"""select vendor,domain,count_all,round((2xx_3xx_count/count_all)*100,4)
as availability,avg_response_time,round((more_than1s_count/count_all)*100,4)
as more_than1s_per,round((404_count/count_all)*100,4) as 404_ratio from
cdn_qos  where date='%s' and count_all > 10000  order by count_all desc
   """

접근 속도 가 가장 느 린 url 분석:
"""SELECT *  FROM (
                                                                                                                    
SELECT concat(host,regexp_extract(request,'.+? +(.+?)(?:\\\?| )+.*',1)),
COUNT(1) AS COUNT_ALL,round(avg(request_time),2) AS avg_rt FROM
viplog.dw_nginx_log WHERE dt='%s'
and host='%s' and (status rlike '^2.*' or status rlike '^3.*')  and
substr(time_local,14,5) >= '09:00' and substr(time_local,14,5)<='21:00'
GROUP BY concat(host,regexp_extract(request,'.+? +(.+?)(?:\\\?| )+.*',1))
        )a where  a.COUNT_ALL > 10 and a.avg_rt > 0.01  ORDER BY a.avg_rt DESC limit 10
    """

특정한 업무 의 각 인터페이스 에 대한 qos 분석:
"""
select a.service,a.api,a.cnt,b.avaible,avg_rt,more1s_ratio,404_ratio from
(
select regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2) as service,
regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2) as api,
count(1)  as cnt from viplog.dw_nginx_log where dt='%s' and host='xxxx' and
regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2) != '' and
regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2) != ''
group by regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2),
regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2)
) a join
(select service,api,cnt,round(2xx_3xx_count*100/cnt,4) as avaible,avg_rt,
round(more_than1s_count*100/cnt,4) as more1s_ratio,round(404_count*100/cnt,4) as
404_ratio FROM
(select regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2) as service,
regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2) as api,count(1) as cnt,
(SUM(IF(SUBSTR(STATUS,0,1)='2',1,0)) + SUM(IF(SUBSTR(STATUS,0,1)='3',1,0))) as 2xx_3xx
_count,    
ROUND(AVG(request_time),4) AS avg_rt,SUM(IF(request_time>1,1,0)) as
more_than1s_count,SUM(IF(STATUS='404',1,0)) AS 404_count
FROM  viplog.dw_nginx_log where dt='%s' and host='xxxx'  and
regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2) != ''
and regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2) != ''
group by regexp_extract(request,'.+? +(.+?)service=(.+?)&(.+) .+?',2),
regexp_extract(request,'.+? +(.+?)api_key=(.+?)&(.+) .+?',2)  order by cnt desc)a)b
on a.service=b.service and a.api=b.api and a.cnt >10000 order by a.cnt desc
    """

2. 현재 사용 하고 있 는 storm + impala 의 조합 을 실시 간 으로 계산 합 니 다.
현 단계 에서 성능 이 비교적 안정 적 인 impala 버 전 은 1.1.1 로 serde 와 udf 를 지원 하지 않 기 때문에 nginx 에 대한 분석 이 편리 하지 않다.
스 톰 은 이런 udf 의 계산 을 할 수 있다.그러나 storm 의 사용 은 특별히 편리 하지 않 아서 둘 이 함께 사용 해 야 더욱 좋 은 효과 가 있다.
스 톰 에 대해 잘 모 르 기 때문에 여 기 는 잠시 소개 하지 않 겠 습 니 다.간단하게 우리 가 impala 로 qos 를 계산 하 는 sql 을 붙 여 주세요.
(serde 가 지원 되 지 않 기 때문에 nginx 로그 필드 를 분석 해 야 합 니 다)
select host,concat(substr(lt,1,18),'01') as st,parse_url(concat('http://',host,regexp_extract(request,'([^ ]*) ([^ ]*)',2)),'PATH') as url,
sum(cast( case when status like '2%' or status like '3%' then body_bytes_sent else '0' end as int)) as  2xx3xx_body_size,
sum(cast( case when status like '4%' then body_bytes_sent else '0' end as int)) as  4xx_body_size,
sum(cast( case when status like '5%' then body_bytes_sent else '0' end as int)) as  5xx_body_size,
sum(cast(body_bytes_sent as int)) as all_body_size ,
sum(cast( case when status like '2%' or status like '3%' then request_time else '0' end as int)) as  2xx3xx_response_time,
sum(cast( case when status like '4%' then request_time else '0' end as int)) as  4xx_response_time,
sum(cast( case when status like '5%' then request_time else '0' end as int)) as  5xx_response_time,
sum(cast(request_time as int)) as all_response_time ,
sum(cast( case when status like '2%' or status like '3%' then 1 else 0 end as int)) as  2xx3xx_count,
sum(cast( case when status like '4%' then 1 else 0 end as int)) as  4xx_count,
sum(cast( case when status like '5%' then 1 else 0 end as int)) as  5xx_count,
sum(1) as all_count
from (
select regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 3) as lt,regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 4) as request,
regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 11) as    host,regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 6)   as status,trim(regexp_replace(regexp_replace(regexp_replace(regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 5),'\\"',''),'\\.',''),'^0*','')) as request_time,regexp_extract(line,'([^ ]*) - ([^ ]*) \\\[(.*)\\\] "([^"]*)" ("[^"]*" )?(-|[0-9]*) (-|[0-9]*) "([^"]*)" "([^"]*)" (.*) ([^ ]*com) ([^ ]*)($| ([^ ]*))', 7) as body_bytes_sent
from ods_nginx_log_5min_impala)tmp where host !='' and length(host)<=40 and (request !='-' or status !='400') and lower(request) not like '%\.jpg%' and lower(request) not like '%\.ico%' and lower(request) not like '%\.gif%' and lower(request) not like '%\.swf%' and lower(request) not like '%\.txt%' and lower(request) not like '%\.html%' and  lower(request) not like '%\.js%' and lower(request) not like '%\.css%' and lower(request) not like '%\.png%' group by host,st,parse_url(concat('http://',host,regexp_extract(request,'([^ ]*) ([^ ]*)',2)),'PATH');

좋은 웹페이지 즐겨찾기