Pascal · projectA temperature chart of the NAS in Free Pascal
This program shows how warm the NAS is: one bar for the system, one for the CPU and one for each disk bay, on a scale up to 70 °C. A bar is green below 45 °C, amber from 45 °C and red from 55 °C, and an empty bay says "empty".
Temperatures of TBS-h574TX 70 °C ┤ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ 60 °C ┤ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ▄▄▄▄ 50 °C ┤ ┈┈┈┈ ████ ████ ▄▄▄▄ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ████ ████ ████ 40 °C ┤ ┈┈┈┈ ████ ████ ████ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ▄▄▄▄ ████ ████ ████ 30 °C ┤ ████ ████ ████ ████ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ████ ████ ████ ████ 20 °C ┤ ████ ████ ████ ████ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ████ ████ ████ ████ 10 °C ┤ ████ ████ ████ ████ ┈┈┈┈ ┈┈┈┈ ┈┈┈┈ │ ████ ████ ████ ████ └──────────────────────────────────────────────────────── System CPU Disk 1 Disk 2 Disk 3 Disk 4 Disk 5 34 °C 53 °C 50 °C 48 °C empty empty empty ██ below 45 °C ██ 45 to 54 °C ██ 55 °C and above
Download it on the NAS
curl -fsSLO https://kb.nasutils.net/projects/pascal/temps.pastemps.pas Download temps.pas
{ Temperatures: a vertical bar chart of the system, the CPU and every disk bay, from QNAP's getsysinfo.
Build and run: fpc temps.pas && ./temps (prints once and ends)
Green below 45 °C, amber below 55 °C, red from 55 °C. An empty bay shows as "empty". }
program temps;
{$mode objfpc}{$H+}
uses sysutils, unix;
const
Rows = 14; { the chart is 14 lines tall, 5 °C a line, so it reaches 70 °C }
Step = 5;
Cell = 8; { each bar takes 8 columns: 4 of bar and 4 of gap }
type
TSensor = record
Name: string;
Value: integer; { -1 when the bay is empty or the sensor gives no number }
end;
var
sensors: array of TSensor;
model, s: string;
i, r, bays: integer;
function Esc(const code: string): string; begin Result := #27'[' + code + 'm'; end;
function Rep(const s: string; n: integer): string; var i: integer; begin Result := ''; for i := 1 to n do Result := Result + s; end;
{ Letters of a UTF-8 text: every byte except the continuation bytes $80-$BF }
function Width(const s: string): integer; var c: char; begin Result := 0; for c in s do if not (c in [#$80..#$BF]) then Inc(Result); end;
function Center(const s: string; n: integer): string;
begin Result := Rep(' ', (n - Width(s)) div 2) + s + Rep(' ', n - Width(s) - (n - Width(s)) div 2); end;
{ The first line getsysinfo prints for a question, '' when there is no getsysinfo }
function Ask(const question: string): string;
var f: Text;
begin
Result := '';
if POpen(f, 'getsysinfo ' + question + ' 2>/dev/null', 'R') <> 0 then Exit;
if not Eof(f) then ReadLn(f, Result);
PClose(f);
end;
{ "50 C/122 F" gives 50; "-- C/-- F" and an empty answer give -1 }
procedure Add(const name, answer: string);
begin
SetLength(sensors, Length(sensors) + 1);
sensors[High(sensors)].Name := name;
sensors[High(sensors)].Value := StrToIntDef(Copy(answer, 1, Pos(' ', answer + ' ') - 1), -1);
end;
function Level(t: integer): string;
begin
if t >= 55 then Result := '31' else if t >= 45 then Result := '33' else Result := '32';
end;
begin
model := Ask('model');
if model = '' then
begin
WriteLn('getsysinfo is missing: this program reads the sensors of a QNAP NAS.');
Halt(1);
end;
Add('System', Ask('systmp'));
Add('CPU', Ask('cputmp'));
bays := StrToIntDef(Ask('hdnum'), 0);
for i := 1 to bays do Add('Disk ' + IntToStr(i), Ask('hdtmp ' + IntToStr(i)));
WriteLn;
WriteLn(' ', Esc('1;33'), 'Temperatures of ', model, Esc('0'));
WriteLn;
for r := Rows downto 1 do
begin
if r mod 2 = 0 then s := Esc('33') + Format('%3d °C ┤', [r * Step]) else s := Esc('33') + ' │';
for i := 0 to High(sensors) do
begin
s := s + ' ';
if sensors[i].Value >= r * Step then
s := s + Esc(Level(sensors[i].Value)) + '████'
else if sensors[i].Value >= r * Step - Step div 2 then
s := s + Esc(Level(sensors[i].Value)) + '▄▄▄▄'
else if r mod 2 = 0 then
s := s + Esc('90') + '┈┈┈┈'
else
s := s + ' ';
s := s + Esc('0') + ' ';
end;
WriteLn(s);
end;
WriteLn(Esc('33'), ' └', Rep('─', Cell * Length(sensors)), Esc('0'));
s := ' '; for i := 0 to High(sensors) do s := s + Center(sensors[i].Name, Cell);
WriteLn(Esc('1'), s, Esc('0'));
s := ' ';
for i := 0 to High(sensors) do
if sensors[i].Value < 0 then s := s + Esc('90') + Center('empty', Cell)
else s := s + Esc(Level(sensors[i].Value)) + Center(IntToStr(sensors[i].Value) + ' °C', Cell);
WriteLn(s, Esc('0'));
WriteLn;
WriteLn(' ', Esc('32'), '██', Esc('0'), ' below 45 °C ', Esc('33'), '██', Esc('0'), ' 45 to 54 °C ',
Esc('31'), '██', Esc('0'), ' 55 °C and above');
end.
What you learn
- A vertical chart prints from the top line down: each line asks of every bar "does it reach this high?".
▄▄▄▄draws the lower half of a line, so a bar can end halfway up a line of 5 °C.StrToIntDef(..., -1)turns "50 C/122 F" into 50 and "-- C/-- F" into -1, the mark of an empty bay.- Constants such as
Rows,StepandCellhold the size of the chart in one place.
How it works
The program needs getsysinfo, QNAP's own tool in /sbin. Without it, it says so and ends with code 1. It asks for the model, systmp, cputmp and hdnum, the number of disk bays, and then hdtmp 1 to hdtmp N. Each answer becomes a TSensor record with a name and a value.
The chart has 14 lines of 5 °C each. For line r the top is r * 5 °C. A bar at least that warm gets a full block, a bar within 2 °C below it a half block, and an empty place gets a dotted grid line on every second line. The scale on the left names every 10 °C.
Under the axis come the names, centred in 8 columns each, then the values in the colour of their level, and a legend of the three colours.
Notes to the lines
- 9–11The chart is 14 lines tall at 5 °C a line, so it ends at 70 °C; each bar takes 8 columns.
- 13–16A sensor is a name and a number, -1 when the bay is empty.
- 24–25
Widthcounts letters, so "°C" counts as two columns and the names stand centred. - 30–38
Askrunsgetsysinfowith one question and returns the first line of the answer. - 44The number before the first space;
StrToIntDefgives -1 when it is not a number, as with "--". - 47–50The colour of a level: red from 55 °C, amber from 45 °C, green below.
- 53–58On a PC without
getsysinfothe program stops with a message and code 1. - 61–62One bar for each bay that
hdnumreports, also for empty ones. - 69Every second line carries its temperature on the scale.
- 73–80Full block, half block, grid line or empty: the four looks of one place of the chart.
- 90–91An empty bay shows "empty" in grey; a value shows in its colour.
Run it
fpc temps.pas && ./tempsThe program prints the chart and ends.
Try it yourself
Set Step = 4 on line 10 to make the chart reach 56 °C with finer lines. Print a line under the legend with the number of fans from getsysinfo sysfannum.
More projects in Pascal
- A menu bar with drop-down boxes in Free PascalA Free Pascal program draws a desktop with a menu bar, drop-down boxes and dialog boxes with shadows, and fills the dialogs with the NAS's memory, load, processor, volumes and RAID groups. You learn to draw boxes with escape codes, read keys with crt and run commands with POpen.
- A greeting screen with large letters in Free PascalA Free Pascal program prints the word NAS in large block letters, greets you by the hour and lists the model, kernel, uptime, load and memory in a framed table. You learn records and dynamic arrays, reading the date with dateutils and drawing a table with box characters.
- A NAS summary in two columns in Free PascalA Free Pascal program reads the CPU, temperatures, load, memory, data volumes and network ports into records and prints them as framed panels in two columns. You learn records, dynamic arrays of records, var parameters and a layout built from lists of lines.
- Cable run: a moving game in Free PascalA Free Pascal game where you steer a network cable round a frame with the arrow keys; it grows with every data packet it picks up, and the wall or its own body ends the run. You learn a game loop with KeyPressed and Delay, records for coordinates and drawing only the cells that change.
- A temperature chart of the NAS in Free PascalA Free Pascal program asks QNAP's getsysinfo for the system, CPU and disk temperatures and draws them as a vertical bar chart with a scale, coloured green, amber or red by level. You learn to build a chart line by line from the top and to treat an empty disk bay as a value of its own.
- A log viewer with colours and search in Free PascalA Free Pascal program shows a log file a screen at a time, colours warnings amber and errors red, scrolls with the arrow and page keys and jumps to the next error with /. You learn to load a file into a TStringList, keep a window of lines on the screen and search with UpperCase and Pos.