Scripting Language
Scripting Language
Script Outline
The general outline of a Pikt script is:
<script name> init status active| inactive level emergency| urgent| critical| ... task "<text>" input proc| "<process>" file| "<file>" logfile "<logfile>" filter "<process>" seps "<char(s)>" dat <var> <spec> ... keys <var> [...] begin <statement> ... rule <statement> ... ... end <statement> ...
Init Section
In the init section, you lay the basis for subsequent script actions.
The alarm is given one of eight severity levels, analogous to syslog's severity levels.
The primary alarm input is the output of a process, the full contents of a text file, or logfile updates (new info since the previous alarm run). If a process, it can be any system process (including multiple processes tied together by pipes) yielding text output. Pikt does not deal with binary input. Input may also be passed through an optional filter.
One or more dat statements map input data to variables. The dat statement takes one of three forms:
dat <var> x [ordinal] dat <var> x,y [columnar] dat "<regexp>"
For ordinal input, "seps" specifies a field separator (or separators) other than the default (whitespace).
Concluding the init section, the optional keys line lists variables used as database lookup keys when referring to history values (values stored from previous script runs).
Begin, End, and Rule Sections
Next come action statements, grouped into begin, end, and rule sections.
The heart of a Pikt script is the main processing loop: A line of input (from a proc, file, or logfile) is read in, then acted upon, the next line is read in and acted upon, and so on until the input is exhausted. Before input processing, you might have a begin section, to initialize some variables or take some other preliminary actions. You might also have an end section for input processing followup. (You can achieve additional data processing loops within a Pikt script using a combination of #fopen(), #popen(), #read(), and #fclose(), and/or #pclose().) In other words:
begin [optional] <statement> <statement> ... [while there's input] [optional] rule <statement> <statement> ... ... [endwhile] end [optional] <statement> <statement> ...
The input processing loop consists of one or more rule sections. A rule section usually groups together program statements pertaining to a single attribute of the current input line. Strictly speaking, there is never a need to break up the set of input processing statements into separate rule sections, but doing so helps clarify program logic.
Data Types
Pikt has three basic data types: strings, numbers, and file handles (or proc handles).
Pikt supports both "associative" (string-indexed) and numeric (numerically- indexed) arrays. Array indices are computable (e.g., a concatenation, or the sum of two functions). Numerical arrays are (for now) limited to at most three dimensions. Note that, unlike with many other languages, Pikt array indices start at 1, not 0. (In Pikt, generally speaking, all indexing, in whatever context, begins with 1.)
Variables come in three different time forms. "$" and "#" as variable prefixes refer to current values (strings and numbers respectively). "@" (e.g., ) signifies the value for this variable for the preceding input line. "%" (e.g., %usage) signifies the value for this variable during the previous script run.
So, in Pikt, there is no need to save input data values from one line to the next. Values from the previous input loop are stored automatically for you. The same is true with so-called "history variables." Pikt stores values in a data file for recall the next time the alarm script runs. If a value is tied to a particular input data variable (specified in a dat statement) and a particular line of input, Pikt does a keyword lookup (specified in a keys statement) to find the appropriate data value.
Other Language Features
In general, every Pikt object serves a semantic purpose. Hence, and for example, parentheses are not required around an if condition or the arguments to a for statement. Nor are semicolons or end-of-lines required to signal the end of a program statement.
Pikt provides the usual operators, and a few not so usual. They mostly follow the Perl and AWK models.
Pikt offers a wide variety of built-in functions. An unusual feature of Pikt functions is that they are data-typed: their return value type is signified by either the "$" prefix (for string; e.g., $trim()) or "#" prefix (for number; e.g., #median()). Pikt does not currently support user-definable functions, although you can write pseudo functions using macros-with-arguments to achieve much the same effect.
Pikt comes with a panoply of flow control structures, most usual, and a few not so usual (e.g., 'again', for repeating the current rule; 'leave', for leaving the current rule). Every Pikt statement begins with a keyword (e.g., 'set', 'if', etc.). Statement blocks are indicated by a keyword-keyword combination, for example, if-endif, for-endfor.
Pikt uses AWK and GNU RX-style regular expressions.
prev page | 1st page | next page |