るいすのブログ

オタクエンジニアの雑記

lm_sensors の取得したデータを Twitter に投げてみる


ラックサーバーを買ったおかげでCPUの温度とメモリの温度と家が燃えないかっていう不安に駆られる毎日です。

 

[note]

部屋にラックサーバーを置いたら家を追い出された。「DELL PowerEdge 2950」を買ってみた。

「DELL PowerEdge 2950」のベンチマークをとってみたけどコスパは悪いかもしれない(分かってた)

[/note]

 

 

lm_sensors とは

温度、電圧、ファンを監視するフリーでオープンソースなツールとドライバです。

実行してみると以下のような結果を返してきます。

[source]

i5k_amb-isa-0000 Adapter: ISA adapter Ch. 0 DIMM 0: +59.5°C (low = +105.0°C, high = +124.0°C) Ch. 1 DIMM 0: +53.0°C (low = +105.0°C, high = +124.0°C) Ch. 2 DIMM 0: +59.0°C (low = +105.0°C, high = +124.0°C) Ch. 3 DIMM 0: +59.5°C (low = +105.0°C, high = +124.0°C)

coretemp-isa-0000 Adapter: ISA adapter Core 0: +49.0°C (high = +84.0°C, crit = +100.0°C) Core 1: +49.0°C (high = +84.0°C, crit = +100.0°C) Core 2: +48.0°C (high = +84.0°C, crit = +100.0°C) Core 3: +51.0°C (high = +84.0°C, crit = +100.0°C)

coretemp-isa-0001 Adapter: ISA adapter Core 0: +34.0°C (high = +84.0°C, crit = +100.0°C) Core 1: +30.0°C (high = +84.0°C, crit = +100.0°C) Core 2: +33.0°C (high = +84.0°C, crit = +100.0°C) Core 3: +31.0°C (high = +84.0°C, crit = +100.0°C)

[/source]

 

 

スクレイピングする方法

を迷った。 今回も PHP で書くことにした。

sensorsをリダイレクションでテキストファイルに起こす。

[source]

sensors > temp.txt

[/source]

余計な文字がありすぎてとりあえず数字だけ取り出そうってことにした。

 

[source]

$file = file_get_contents('temp.txt'); $new_str = mb_ereg_replace('[^0-9]','.', $file);

[/source]

とりあえず数値だけを取り出してきて数字以外を全部 . (ピリオド)に置き換える。

 

[source]

.5..........0000..........................0......0....59.5.............105.0............124.0........1......0....54.0.............105.0............124.0........2......0....59.5.............105.0............124.0........3......0....60.0.............105.0............124.0..................0000...........................0.........50.0.............84.0............100.0.........1.........50.0.............84.0............100.0.........2.........48.0.............84.0............100.0.........3.........51.0.............84.0............100.0..................0001...........................0.........34.0.............84.0............100.0.........1.........30.0.............84.0............100.0.........2.........33.0.............84.0............100.0.........3.........30.0.............84.0............100.0.....

[/source]

こんな感じになる。

今回固定されてる数字が ”0000”, "'105.0', '124.0", 最初の"5", "84", とかそこら辺を消し去ってみる。

 

[source]

$search = array('0000', '105.0', '124.0', '.5....................................0......0....', '.................................1......0....', '.................................2......0....', '.................................3......0....', '......................................................................0.........', '......................................................................0.........', '.............84.0............100.0.........1.........', '.............84.0............100.0.........2.........', '.............84.0............100.0.........3.........', '.............84.0............100.0..................0001...........................0.........', '.............84.0............100.0.........1.........', '.............84.0............100.0.........2.........', '.............84.0............100.0.........3.........', '.............84.0............100.0.....'); $new_str1 = str_replace($search, '', $new_str);

[/source]

読み取りたい数値とか文字(ピリオド)を力技で消してく。 するとこんな感じに。 ”59.554.059.560.050.050.048.051.034.030.033.030.0”

 

[source]

$array = str_split($new_str1, 4);

[/source]

こうすれば4文字ずつ配列に入っていってくれるので echo $array[0]; とかやれば一枚目のメモリの温度が出てくる。

 

 

結果

 

結果的にこうなる。

https://github.com/rluisr/lm_sensors_Twitter