Rootop 服务器运维与web架构

2019-01-17
发表者 Venus
mysql临时开启慢日志记录已关闭评论

mysql临时开启慢日志记录

在不方便重启mysql情况下可以通过设置全局变量实现开启:

# 查询当前状态

SHOW VARIABLES LIKE '%slow_query%';
SHOW VARIABLES LIKE '%long_query_time%';

# 设置开启,设置sql执行时间超过3秒则记录

SET GLOBAL slow_query_log = ON;
SET GLOBAL long_query_time = 3;
SET GLOBAL slow_query_log_file = "/usr/local/mysql/data/mysql-slow.log";

# 注意 slow_query_log_file 指定的目录要存在,并且mysql要有权限写入,否则会报错如下:
Variable ‘slow_query_log_file’ can’t be set to the value of ‘/usr/local/mysql/mysql-slow.log’

PS:
需要断开mysql客户端再次连接,执行SHOW VARIABLES LIKE ‘%long_query_time%’;才能看到修改后的时间。否则还是之前的时间。

mysql中其它的日志:

# 普通日志

SHOW VARIABLES LIKE '%general_log%'; # general_log、general_log_file
SET GLOBAL general_log='OFF';

# 错误日志

SHOW VARIABLES LIKE '%log_error%';   # log_error
SET GLOBAL log_error='/usr/local/mysql/data/mysql.err';

# 二进制日志

SHOW VARIABLES WHERE `variable_name` LIKE '%log_bin%' OR `variable_name` LIKE '%binlog%';

2019-01-16
发表者 Venus
利用outpu插件实现将日志提交到指定url,并处理报警。已关闭评论

利用outpu插件实现将日志提交到指定url,并处理报警。

步骤简单记录。
java安装略
logstash5.4 解压版,安装略
前提条件:日志为json格式。
这里手动将nginx的日志拼为json。

input {
    file {
        type => "nginx_log"
        path => "/var/log/nginx/access.log"
    }
}

# 将message字段的数据(json格式) "导入",供下面if判断调用。
filter {
    json {
        source => "message"
    }        
}

output {
    # http 模块会将数据提交到指定url
    if [loglevel] == "error" {
        http {
            http_method => "post"
            url => "https://www.rootop.org/xxx.php"
        }
    }

    elasticsearch {
        hosts => "192.168.10.71"
        index => "nginx-access-%{+YYYY.MM.dd}"
    }
 
    stdout {
        codec => rubydebug
    }
}

path => “/var/log/nginx/access.log” 支持通配符,如*.log ,实现一天一个日志文件,并可以持续采集日志
路径中也可以带通配符

2019-01-16
发表者 Venus
centos7 进入单用户模式已关闭评论

centos7 进入单用户模式

改了一个配置文件导致无法进入系统,想通过进单用户模式改回去,发现跟centos5的方法不一样了。记录一下。

重启服务器,出来grub菜单以后按上下键对启动菜单进行编辑。

按字母 e 进行编辑。

找到倒数第二行配置,也就是linux16开头那个,把ro改为rw,方便保存编辑的文件,否则默认是只读文件系统。最后面追加一句 init=/bin/sh

ctrl+x 开始启动。

2019-01-08
发表者 Venus
network-status中状态为 blocked:mixed-content已关闭评论

network-status中状态为 blocked:mixed-content

谷歌浏览器开发工具console中报:
Mixed Content: The page at ‘https://www.xxx.cn/wp/wp-login.php’ was loaded over HTTPS, but requested an insecure stylesheet ‘http://www.xxx.cn/wp/wp-admin/load-styles.php?c=0&dir=ltr&load%5B%5D=dashicons,buttons,forms,l10n,login&ver=4.8.2’. This request has been blocked; the content must be served over HTTPS.

nginx虚拟主机中添加一行:
add_header Content-Security-Policy upgrade-insecure-requests;

2019-01-08
发表者 Venus
fluentd收集容器stdout日志已关闭评论

fluentd收集容器stdout日志

docker支持fluentd日志格式。

--log-driver="json-file|syslog|journald|gelf|fluentd|awslogs|splunk|etwlogs|gcplogs|none"

# 起一个fluentd容器,容器发送过来的日志存到宿主机的/fluentd/log目录下

[root@localhost ~]# docker pull docker.io/fluent/fluentd
[root@localhost ~]# docker run -dit --name fluentd -p 24224:24224 -p 24224:24224/udp -v /fluentd/log:/fluentd/log docker.io/fluent/fluentd

# 起一个容器,日志发往fluentd。

[root@localhost ~]# docker run -dit --name test --log-driver=fluentd --log-opt fluentd-address=127.0.0.1:24224 --log-opt tag='test-1' centos ping www.baidu.com

# 指定日志格式后,就不再支持docker logs查看日志了。

[root@localhost log]# docker logs test
"logs" command is supported only for "json-file" and "journald" logging drivers (got: fluentd)

# 配置filebeat,把日志发往elasticsearch

安装 https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-x86_64.rpm
编辑配置文件,修改:
[root@localhost ~]# vi /etc/filebeat/filebeat.yml 
# 需要收集的日志路径
filebeat.inputs:

# Each - is an input. Most options can be set at the input level, so
# you can use different inputs for various configurations.
# Below are the input specific configurations.

- type: log

  # Change to true to enable this input configuration.
  enabled: true

  # Paths that should be crawled and fetched. Glob based paths.
  paths:
    - /fluentd/log/*.log
    
# 发往elasticsearch地址
output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["192.168.10.17:9200"]

# 注意
如果paths中指定了一个软连接文件,filebeat是不会收集的。(因为这个问题查了半天)

# 重启filebeat

[root@localhost ~]# systemctl restart filebeat

这样es中就自动创建了索引,以filebeat-6.5.4-日期 开头。