# How to install, update and remove SSH Monitor

## How to install

* **Prerequisites**:
  * [Gawk](https://repology.org/project/gawk/versions) should be installed
    on the {{SSH_Monitor}} {{host}} computer, in order to be able to run `awk` in a shell.
  * [Iputils](https://repology.org/project/iputils/versions) should be installed
    on the {{SSH_Monitor}} {{host}} computer, in order to be able to run `ping` in a shell.
  * [Openssh](https://repology.org/project/openssh/versions) should be installed
    on the {{SSH_Monitor}} {{host}} computer, in order to be able to run `ssh` in a shell.
  * The {{EPICS}} base `v7` should be installed on the {{SSH_Monitor}}
    {{host}} computer like described in the official documentation:
    <https://docs.epics-controls.org/projects/how-tos/en/latest/getting-started/installation.html>
    (note that you can install the {{EPICS}} base e.g. in `/opt/epics/base` instead of
    `$HOME/EPICS/epics-base`, or anywhere else).
    * The {{EPICS}} base should be at least `v7.0.7+` in order to avoid the
      [aSub constant input bug](https://github.com/epics-base/epics-base/issues/284).

* After installing the {{EPICS}} base on the {{host}} computer, you will first have
  to patch it in order to increase the maximum size of a {{macro}}. By default,
  the maximum size of a {{macro}} is 256 characters long, which is too short
  for the common {{SSH_Monitor}} use case. Let's increase it to 65535:

    ```{code} bash
    $ cd /opt/epics/base

    $ sed -i 's/MAC_SIZE 256/MAC_SIZE 65535/' modules/libcom/src/macLib/macLib.h
    $ sed -i 's/MY_BUFFER_SIZE 1024/MY_BUFFER_SIZE 65535/' modules/database/src/ioc/dbStatic/dbLexRoutines.c
    $ sed -i 's/MAX_VAR_FACTOR 50/MAX_VAR_FACTOR 1000/' modules/database/src/ioc/dbtemplate/dbLoadTemplate.y
    $ sed -i 's/MAXLINE BUFSIZ/MAXLINE 65535/' modules/libcom/src/flex/flexdef.h
    $ sed -i 's/YY_TRAILING_MASK 0x2000/YY_TRAILING_MASK 0x20000/' modules/libcom/src/flex/flexdef.h
    $ sed -i 's/YY_TRAILING_HEAD_MASK 0x4000/YY_TRAILING_HEAD_MASK 0x40000/' modules/libcom/src/flex/flexdef.h
    $ sed -i 's/YY_READ_BUF_SIZE 8192/YY_READ_BUF_SIZE 65535/' modules/libcom/src/flex/flex.skel
    $ sed -i 's/YY_READ_BUF_SIZE 8192/YY_READ_BUF_SIZE 65535/' modules/libcom/src/flex/scan.c
    $ sed -i 's/YY_READ_BUF_SIZE 8192/YY_READ_BUF_SIZE 65535/' modules/libcom/src/flex/flex.skel.static

    $ make clean && make && echo OK || echo KO
    ```

* On the {{host}} computer, clone {{SSH_Monitor}} somewhere (e.g. in
  `/opt/epics/support`):

    ```{code} bash
    $ mkdir -p /opt/epics/support
    $ cd /opt/epics/support
    $ git clone git@gitlab.com:sshmonitor/sshmonitor.git

    $ cd sshmonitor
    $ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
    $ git checkout $latestTag # checkout to the latest release

    $ vi configure/RELEASE # configure the path to your EPICS base location
        > ...
        >
        > # EPICS_BASE should appear last so earlier modules can override stuff:
      ~ > EPICS_BASE = /path/to/your/epics/base
        >
        > # Set RULES here if you want to use build rules from somewhere
        > # other than EPICS_BASE:
        > #RULES = $(MODULES)/build-rules
        >
        > # These lines allow developers to override these RELEASE settings
        > # without having to modify this file directly.
        > -include $(TOP)/../RELEASE.local
        > -include $(TOP)/../RELEASE.$(EPICS_HOST_ARCH).local
        > -include $(TOP)/configure/RELEASE.local

    $ make && echo OK || echo KO
    ```

* On the {{host}} computer, create the {{Top}} that will
  monitor your {{target}}(s) and import {{SSH_Monitor}} into it:

    ```{code} bash
    $ mkdir myTargetMonitoringTop
    $ cd myTargetMonitoringTop
    $ makeBaseApp.pl -a linux-x86_64 -t ioc myTargetMonitoring
    $ makeBaseApp.pl -a linux-x86_64 -i -p myTargetMonitoring MyTargetMonitoring

    $ vi configure/RELEASE
        > ...
        >
        > # Variables and paths to dependent modules:
        > #MODULES = /path/to/modules
        > #MYMODULE = $(MODULES)/my-module
        >
        > # If using the sequencer, point SNCSEQ at its top directory:
        > #SNCSEQ = $(MODULES)/seq-ver
      + > SSHMONITOR = /opt/epics/support/sshmonitor
        >
        > # EPICS_BASE should appear last so earlier modules can override stuff:
        > EPICS_BASE = /path/to/your/epics/base
        >
        > # Set RULES here if you want to use build rules from somewhere
        > # other than EPICS_BASE:
        > #RULES = $(MODULES)/build-rules
        >
        > # These lines allow developers to override these RELEASE settings
        > # without having to modify this file directly.
        > -include $(TOP)/../RELEASE.local
        > -include $(TOP)/../RELEASE.$(EPICS_HOST_ARCH).local
        > -include $(TOP)/configure/RELEASE.local

    $ vi myTargetMonitoringApp/Db/Makefile
        > TOP=../..
        > include $(TOP)/configure/CONFIG
        > #----------------------------------------
        > #  ADD MACRO DEFINITIONS AFTER THIS LINE
        >
        > #----------------------------------------------------
        > # Create and install (or just install) into <top>/db
        > # databases, templates, substitutions like this
        > #DB += xxx.db
        >
      + > DB_INSTALLS += $(SSHMONITOR)/db/ssh_monitor_core.template
      + > DB_INSTALLS += $(wildcard $(SSHMONITOR)/db/*.substitutions)
        >
        > #----------------------------------------------------
        > # If <anyname>.db template is not named <anyname>*.template add
        > # <anyname>_template = <templatename>
        >
        > include $(TOP)/configure/RULES
        > #----------------------------------------
        > #  ADD RULES AFTER THIS LINE

    $ vi myTargetMonitoringApp/src/Makefile
        > TOP=../..
        >
        > include $(TOP)/configure/CONFIG
        > #----------------------------------------
        > #  ADD MACRO DEFINITIONS AFTER THIS LINE
        > #=============================
        >
        > #=============================
        > # Build the IOC application
        >
        > PROD_IOC = myTargetMonitoring
        > # myTargetMonitoring.dbd will be created and installed
        > DBD += myTargetMonitoring.dbd
        >
        > # myTargetMonitoring.dbd will be made up from these files:
        > myTargetMonitoring_DBD += base.dbd
      + > myTargetMonitoring_DBD += system.dbd
        >
        > # Include dbd files from all support applications:
        > #myTargetMonitoring_DBD += xxx.dbd
      + > myTargetMonitoring_DBD += sshmonitorSupport.dbd
        >
        > # Add all the support libraries needed by this IOC
        > #myTargetMonitoring_LIBS += xxx
      + > myTargetMonitoring_LIBS += sshmonitorSupport
        >
        > # myTargetMonitoring_registerRecordDeviceDriver.cpp derives from myTargetMonitoring.dbd
        > myTargetMonitoring_SRCS += myTargetMonitoring_registerRecordDeviceDriver.cpp
        >
        > # Build the main IOC entry point on workstation OSs.
        > myTargetMonitoring_SRCS_DEFAULT += myTargetMonitoringMain.cpp
        > myTargetMonitoring_SRCS_vxWorks += -nil-
        >
        > # Add support from base/src/vxWorks if needed
        > #myTargetMonitoring_OBJS_vxWorks += $(EPICS_BASE_BIN)/vxComLibrary
        >
        > # Finally link to the EPICS Base libraries
        > myTargetMonitoring_LIBS += $(EPICS_BASE_IOC_LIBS)
        >
        > #===========================
        >
        > include $(TOP)/configure/RULES
        > #----------------------------------------
        > #  ADD RULES AFTER THIS LINE

    $ make && echo OK || echo KO
    ```

---

## How to update

* Pull the latest changes and re-build the {{SSH_Monitor}} {{Top}}:

    ```{code} bash
    $ cd /path/to/sshmonitor # e.g. `/opt/epics/support/sshmonitor`, like in the previous section
    $ git checkout master
    $ git pull
    $ git pull --tags
    $ latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
    $ git checkout $latestTag # checkout to the latest release
    $ make clean && make
    ```

---

## How to remove

* Just delete the whole {{SSH_Monitor}} {{Top}}:

    ```{code} bash
    $ rm -rf /path/to/sshmonitor # e.g. `/opt/epics/support/sshmonitor`, like in the previous section
    ```
