王筝的博客
ruby学习

Using AWS CloudWatch in Grafana

https://grafana.com/docs/features/datasources/cloudwatch/

http://IP:3000/datasources 在这个页面可以查看之前设置过的所有数据源


点击“Add data source”可以添加一个新的数据源, 注意类型选择 CloudWatch

Access key ID 和 Secret access Key 需要在AWS的  Identity and Access Management 页面里配置用户和权限

控制台网址: https://console.aws.amazon.com/iam/home?#/home

中文教程:https://docs.aws.amazon.com/zh_cn/general/latest/gr/managing-aws-access-keys.html

英文教程: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_access-keys.html#Using_CreateAccessKey_API

填写完点击“Save & Test”, 如果保存成功并且能够连接到这个dataSource的话,页面会有提示


dashborad 模板下载:https://codeload.github.com/monitoringartist/grafana-aws-cloudwatch-dashboards/zip/master

解压到本地。

通过import导入模板来新建一个dashboard。


找到刚才下载的文件夹,选择里面的“aws-efs.json”,



点击import,新的dashboard就创建完成了,需要注意的是Region是可选的,只有你的efs所在的region才有数据。

public void doGet(HttpServletRequest request, HttpServletResponse response)
{
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=\"userDirectory.csv\"");
    try
    {
        OutputStream outputStream = response.getOutputStream();
        String outputResult = "xxxx, yyyy, zzzz, aaaa, bbbb, ccccc, dddd, eeee, ffff, gggg\n";
        outputStream.write(outputResult.getBytes());
        outputStream.flush();
        outputStream.close();
    }
    catch(Exception e)
    {
        System.out.println(e.toString());
    }
}

(一) 安装prometheus install

$ wget https://github.com/prometheus/prometheus/releases/download/v2.4.3/prometheus-2.4.3.linux-amd64.tar.gz

$ tar zxvf prometheus-2.4.3.linux-amd64.tar.gz

$ cd prometheus-2.4.3.linux-amd64

如果需要添加new job,在这个文件添加

$ vim prometheus.yml   

global:
  scrape_interval:     15s
  evaluation_interval: 15s
  # scrape_timeout is set to the global default (10s).
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093
rule_files:

scrape_configs:

  - job_name: 'prometheus'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'linux1'
    static_configs:
      - targets: ['192.xxx.xx.xx:9100']
        labels:
          instance: server1
  - job_name: 'linux2'
    static_configs:
      - targets: ['192.xxx.xx.xx:9100']
        labels:
          instance: server2
运行 start Prometheus$ nohup ./prometheus &  

改成systemd服务

$ cd ..
$ sudo mv prometheus-2.4.3.linux-amd64 /usr/local/prometheus

$ sudo groupadd prometheus
$ sudo useradd -g prometheus -m -d /usr/local/prometheus -s /sbin/nologin prometheus

$ sudo chown -R prometheus:prometheus /usr/local/prometheus

$ sudo vim /etc/systemd/system/prometheus.service  # 写入如下内容
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target

Prometheus自带一个Web网站,默认端口为9090, http://ip:9090.可以查看表达式搜索结果、报警配置、prometheus配置,exporter状态等. 

(二) 安装配置Grafana

官方安装文档: https://grafana.com/grafana/download?platform=linux

http://docs.grafana.org/installation/

$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.3.0_amd64.deb 
$ sudo dpkg -i grafana_5.3.0_amd64.deb 

$ sudo systemctl status grafana-server    #默认进入了运行的状态

$ sudo systemctl enable grafana-server

关闭
sudo service grafana-server stop
启动
sudo service grafana-server start

open port 3000端口的访问权限 https://blog.csdn.net/zbljz98/article/details/80464761

$ iptables -A INPUT -p tcp –dport 3000 -j ACCEPT

我开始的时候就是没做这一步,导致无法在ip:3000端口访问

进入登录页面,首次访问的用户名是admin,  密码是 admin

首次登录之后会要求你更换密码