To understand how to code a plugin, we have first to grasp how, on a broad scale, a Nagios check works. There are two different kinds of checks.
Active and passive checks
Active checks
Based on a time range, or manually triggered, an active check sees a plugin actively connecting to a service and collecting information. A typical example could be for a plugin to check the disk space: once invoked, it interfaces with (usually) the operating system, executes a df command, works on the output, extracts the value related to the disk space, evaluates it against some thresholds, and reports back a status, such as OK, WARNING, CRITICAL, or UNKNOWN.