增加CPU使用率和服务器响应时间

时间:2014-11-15 05:39:58

标签: apache ubuntu memory memcached cpu-usage

在过去的10天里,我的网站CPU使用率突然升高,CPU使用率达到100%三倍。在那段时间里,我无法通过SSH连接到我的网站,因此我无法弄清楚实际上是在使用那么多CPU。我的网站是一个使用Apache2,PostgreSQL和Memcached的Django应用程序,托管在DigitalOcean上(1GB Ram,30GB SSD Disk,Ubuntu 14.04 x64)。

这是当前top的输出。

top - 11:00:37 up  8:40,  1 user,  load average: 0.18, 0.21, 0.22
Tasks:  81 total,   2 running,  79 sleeping,   0 stopped,   0 zombie
%Cpu(s): 24.3 us,  2.3 sy,  0.0 ni, 69.1 id,  4.0 wa,  0.3 hi,  0.0 si,  0.0 st
KiB Mem:   1017948 total,   918656 used,    99292 free,   124816 buffers
KiB Swap:        0 total,        0 used,        0 free.   205868 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
 1084 www-data  20   0  833820 218516   5808 S 22.2 21.5   4:22.42 apache2
 3504 postgres  20   0  249560  22844  20036 S  3.0  2.2   0:00.09 postgres
  928 memcache  20   0  359596  35764    876 S  0.3  3.5   0:03.90 memcached
 1085 www-data  20   0  842852  14372   3044 S  0.3  1.4   0:19.76 apache2
 3477 root      20   0   24820   1532   1132 R  0.3  0.2   0:00.27 top
    1 root      20   0   33384   2688   1400 S  0.0  0.3   0:10.75 init
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    3 root      20   0       0      0      0 S  0.0  0.0   0:00.08 ksoftirqd/0
    5 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    7 root      20   0       0      0      0 S  0.0  0.0   0:00.91 rcu_sched
    8 root      20   0       0      0      0 R  0.0  0.0   0:01.96 rcuos/0
    9 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
   10 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcuob/0
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
   12 root      rt   0       0      0      0 S  0.0  0.0   0:00.51 watchdog/0
   13 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 khelper
   14 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   15 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns

这是Apache conf文件

Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

<Directory />
    Options FollowSymLinks
    AllowOverride All
    Require all denied
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

这是我的VirtualHost配置

<VirtualHost *:80>

    RewriteEngine On
    RewriteCond %{HTTP_HOST}  ^example.com [nocase]
    RewriteRule ^(.*)         http://www.example.com$1 [last,redirect=301]

    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    WSGIDaemonProcess example python-path=/home/abc/example:/home/abc/example/env/lib/python2.7/site-packages
    WSGIProcessGroup example
    WSGIApplicationGroup %{GLOBAL}
    WSGIScriptAlias / /home/abc/example/wsgi.py

    DocumentRoot /home/abc/example

    <Directory />
        Require all granted
    </Directory>

    Alias /static/ /home/abc/example/static/

    <Directory /home/abc/example/static>
        Order deny,allow
        Allow from all
    </Directory>

    Alias /media/ /home/abc/example/media/

    <Directory /home/abc/example/media>
        Order deny,allow
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

这是我的.htaccess

<FilesMatch "\.(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

<FilesMatch "\.(jpg|png|gif|css|js|json)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

<IfModule mod_mime.c>
    AddType application/javascript          js
    AddType application/vnd.ms-fontobject   eot
    AddType application/x-font-ttf          ttf ttc
    AddType font/opentype                   otf
    AddType application/x-font-woff         woff
    AddType image/svg+xml                   svg svgz 
    AddEncoding gzip                        svgz
</Ifmodule>

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
    AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
</Ifmodule>

以下是过去一个月的CPU,磁盘和带宽使用情况的图表

Monthly CPU Usage Monthly Disk Usage Monthly Bandwidth Usage

正如您所看到的,过去几天CPU使用率飙升了3倍。我不得不重新启动我的Droplet以降低它。 CPU使用率通常在几天前在0-5%之间变化,但现在已经增加。

该网站的响应时间也增加到约4秒。即使CPU负载不高,响应时间也很长。一切似乎都工作正常,直到大约10天,从那以后我没有对我的配置进行任何更改。流量没有增加,每日流量约为1500名访客。如何确定导致此问题的原因?有什么建议吗?

0 个答案:

没有答案