heroku pg:diagnoseをslackに投げる方法

年末の休暇でコード変更がなかったのが原因かわからないが、herokuのpostgresが謎のout of memoryを吐いて止まりまくる現象が起きた。 しかも、成人の日の三連休中で対応が遅れてしまった。問題が当日わからなかったので、プロセスを再起動などの処理をして、1日様子見。 そして、次の日も問題がお昼ごろに起きたので、follower DBをマスタに切り替えて問題は治った。

週末にherokuに問い合わせたものが火曜日に返信がきた。問題は、CPUがバーストしてたのが原因のこと。 モニタしていなかったので、一日に何回かheroku pg:diagnoseを叩き、それをslackに送るワンライナーを書いた。

$ heroku pg:diagnose --app APP_NAME | curl -X POST --data-urlencode   "payload={\"text\": \"$(</dev/stdin)\"}" https://hooks.slack.com/services/xxxxxxxxxxx

cronに設定しようとしたら動かない。。仕方がないのでshellを書いたら動いた。

#!/bin/bash -x

out=`/snap/bin/heroku pg:diagnose --json --app APP_NAME | /usr/bin/jq -r '.checks[] | {"name": .name, "result": .results} | select(.name |test("Burst"))' | egrep 'name|balance' | sed s/\"//g 2>&1`
echo $out 
curl -X POST --data-urlencode "payload={\"text\": \"$out\"}" https://hooks.slack.com/services/T02xxxxxxxxxxxxxxx

apt updateができない(postgresのところで止まる)ときの対処法

Ubuntu14のサーバがあり、Certbotが下記のエラーで更新できないでいた。

Plugins selected: Authenticator nginx, Installer nginx                                                                                              
Attempting to renew cert (xxxxxxx.com) from /etc/letsencrypt/renewal/xxxxx.com.conf produced an unexpected error: ("bad handshake: Error
([('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')],)",). Skipping.                                                     
All renewal attempts failed. The following certs could not be renewed:                                                                              
  /etc/letsencrypt/live/xxxxxxy.com/fullchain.pem (failure)                                                                                                                                                       

単純にバージョンが古いと思い、apt updateだがしかし、Postgresのところで止まる。。

Err http://apt.postgresql.org trusty-pgdg/main amd64 Packages                                                                                       
  404  Not Found [IP: 147.75.85.69 80]                                                                                                              
Ign http://apt.postgresql.org trusty-pgdg/main Translation-en_US                                                                                    
Ign http://apt.postgresql.org trusty-pgdg/main Translation-en                                                                                       
W: Failed to fetch http://apt.postgresql.org/pub/repos/apt/dists/trusty-pgdg/main/binary-amd64/Packages  404  Not Found [IP: 147.75.85.69 80]  

古いのを使っているのは知っている。これを消す方法をググって、格闘すること30分。 /etc/apt/sources.list.d/pgdg.listを変更する方法でスキップできた。

# vi /etc/apt/sources.list.d/pgdg.list
# comment out the line 
# deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

# then,
# apt update ## works!

certbotもapt upgrade certbotしたら動くようになった。

[Rails] すべてのbooleanカラムの値を変更する方法

DEPRECATION WARNING: `.represent_boolean_as_integer=` is now always true, so setting this is deprecated and will be removed in Rails 6.1
# Due to config.active_record.sqlite3.represent_boolean_as_integer = true

Rails6にアップグレードしたらSQliteのデフォルトboolean値の取扱い方が変わったみたい。 なにやら全ての値を変更せねばならず、大変だと思ったので自動化するスクリプトを書いた。

Rails.application.eager_load!
models = ApplicationRecord.descendants.collect { |type| type.name }
models.each{|m|
  col_hash = Object.const_get(m).columns_hash
  col_hash.each{|k,v|
    if col_hash[k].type == :boolean
      Object.const_get(m).where("#{k} = 't'").update_all( {k.to_sym => 1})
      Object.const_get(m).where("#{k} = 'f'").update_all( {k.to_sym => 0})
    end
  }
}

Node app(redashbot)をsystemdで起動する方法

Redash botが使用できなくなったので、代替を探していたらredashbotを見つけて早速導入。 (Redashは買収後元気ありませんね。。)

導入後Rebootに耐えたいよね、ってことでsystemdに登録してみた。以下手順。

serviceファイル作成

 $ sudo vi /etc/systemd/system/redashbot.service

中身は、

[Unit]
Description=Redashbot

[Service]
PIDFile=/tmp/redashbot.pid
Restart=always
KillSignal=SIGQUIT
WorkingDirectory=/home/ubuntu/redashbot
EnvironmentFile=/home/ubuntu/redashbot/bot.env
ExecStart=npm start
User=ubuntu
Group=ubuntu

[Install]
WantedBy=multi-user.target

bot.envは、

SLACK_BOT_TOKEN=xxxx
REDASH_HOST=yyy
SLACK_BOT_TOKEN=

systemdにセットしてスタート

 $ sudo systemctl daemon-reload
 $ sudo systemctl start redashbot

Reboot後も起動するようにする

 $ sudo systemctl enable redashbot