Sunday, January 27, 2013

Running The MTConnect Agent and Simulator on VMWare's Ubuntu

There is often a good reason to have your own agent and your own simulator running for testing MTConnect.  Below is how to do it with VMWare using Linux.  Note that the source code is out at github under MTConnect under the cppagent directory.

The overall key to this is that BOTH the simulator and the agent must be running for this to work AND you must have an Internet connection EVEN if you are running the simulator and agent you will still need access to .xsd file at

Below is from:

Make sure that VMware Network Adapter is set to Bridged (autodetect) and NOT NAT or you will NOT be able to see it outside of the Mac  remember to the ifconfig to get the IP address

If your host computer is on an Ethernet network, this is often the easiest way to give your virtual machine access to that network. Linux and Windows hosts can use bridged networking to connect to both wired and wireless networks.
If you use bridged networking, your virtual machine needs to have its own identity on the network. For example, on a TCP/IP network, the virtual machine needs its own IP address. Your network administrator can tell you whether IP addresses are available for your virtual machine and what networking settings you should use in the guest operating system. Generally, your guest operating system may acquire an IP address and other network details automatically from a DHCP server, or you may need to set the IP address and other details manually in the guest operating system.

If you use bridged networking, the virtual machine is a full participant in the network. It has access to other machines on the network and can be contacted by other machines on the network as if it were a physical computer on the network.

You can install it anywhere you like. if you want to set up an ubuntu tiny you will need the following pieces from aptget:

1. gcc
2. ruby  (had to do an apt-get in order to load this)
3. cmake
4. libxml2
5. libxml2-dev (had to do an apt-get in order to load this)
5. cppunit

create a directory for the build

you need to get libxml2-dev

apt-get install ruby
apt-get install libxml2   (likely will tell you that it is installed
apt-get install libxml2-dev
apt-get update

ubuntu:/MTConnect_Agent_Simulator_DIR#   history |grep apt

     1     12:03     apt-get install libxml2
     2     12:04     apt-get update
     4     12:05     apt-cache search libxml
     5     12:05     apt-cache search libxml2
     6     12:13     apt-get install libxml2-dev
    98     13:03     history | grep apt
#cmake -i  

   * NOTE:  that just doing regular cmake  -- if you just keep hit return on the questions, it will do a proper cmake
   * run this as root
   * I also noticed that you can run cmake -i from the top


Now you can run the simulator using the ruby run_scenario.rb script. There is help built in. The default agent.cfg will also startup using the VMC3Axis.xml devices file and the VMC3Axis-Log.txt file will give you the same scenario as we have on the site. Any other scenarios can be saved by capturing adapter output into a file.

Will Sobel, MTConnect Chief Architect, told me:
"40 students can hit the agent at the same time, probably can support more than 100. if we wanted to have more than than 1000, i would need to rewrite the internals to use epoll and edge-triggered instead of select and remove the service threads with custom context management. never been an issue."

Ubuntu:/MTConnect_Agent_Simulator_DIR# pwd/home/dave/MTConnect_cppagent-version_1.2.0.14_DIR/mtconnect-cppagent-a100593

ubuntu:/MTConnect_Agent_Simulator_DIR# ls

agent             BUILD.TXT        CMakeFiles          lib      simulator  unix
agent_startup_DIR  ChangeLog        cmake_install.cmake     LICENSE.TXT  README.pdf  test         win32
assets             CMakeCache.txt  CMakeLists.txt     Makefile     samples      tools

ubuntu:/MTConnect_Agent_Simulator_DIR/agent_startup_DIR# ls

agent  agent.cfg

ubuntu:/MTConnect_Agent_Simulator_DIR/agent_startup_DIR# more agent.cfg

Devices = ../simulator/VMC-3Axis.xml
AllowPut = true
ReconnectInterval = 1000

Adapters {
   VMC-3Axis {
      Host = localhost
      Port = 7878

# Logger Configuration
    logging_level = debug
    output = cout

NOTICE the two changes in that I made (HOWEVER, I did NOT run the, just ran the agent directly)

ubuntu:/MTConnect_Agent_Simulator_DIR/unix# pwd

ubuntu:/MTConnect_Agent_Simulator_DIR/unix# cp

ubuntu:/MTConnect_Agent_Simulator_DIR/unix# cat

# init.d script with LSB support.
# Copyright (c) 2007 Javier Fernandez-Sanguino
# This is free software; you may redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2,
# or (at your option) any later version.
# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License with
# the Debian operating system, in /usr/share/common-licenses/GPL;  if
# not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
# Provides:          agent
# Required-Start:    $network $local_fs
# Required-Stop:
# Should-Start:      $named
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: MTConnect agent
# Description:       Provides MTConnect web server functionality

# NOTE: I ran this successfully WITHOUT using this schell script, but I did modify in case I need to use it
# I changed below

# I changed below
DAEMON_OPTS="daemonize $AGENT_DIR/agent.cfg"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

DIETIME=10                   # Time to wait for the server to die, in seconds
                            # If this value is set too low you might not
                            # let some servers to die gracefully and
                            # 'restart' will not work

# Include mongodb defaults if available
if [ -f /etc/default/$NAME ] ; then
        . /etc/default/$NAME

# make sure the log directory exists
[ ! -d "$LOGDIR" ] || mkdir -p $LOGDIR

# Check that the user exists (if we set a user)
# Does the user exist?
if [ -n "$DAEMONUSER" ] ; then
    if getent passwd | grep -q "^$DAEMONUSER:"; then
        # Obtain the uid and gid
        DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
        DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
        log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist."
        exit 1

set -e

running_pid() {
# Check if a given process pid's cmdline matches a given name
    [ -z "$pid" ] && return 1
    [ ! -d /proc/$pid ] &&  return 1
    cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
    # Is this the expected server
    [ "$cmd" != "$name" ] &&  return 1
    return 0

running() {
# Check if the process is running looking at /proc
# (works for all users)

    # No pidfile, probably no daemon present
    [ ! -f "$PIDFILE" ] && return 1
    pid=`cat $PIDFILE`
    running_pid $pid $DAEMON || return 1
    return 0

start_server() {
# Start the process using the wrapper
        if [ -z "$DAEMONUSER" ] ; then
            start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
                        --make-pidfile --exec $DAEMON -- $DAEMON_OPTS

# if we are using a daemonuser then change the user id
            start-stop-daemon --background --start --quiet --pidfile $PIDFILE \
                        --make-pidfile --chuid $DAEMONUSER \
                        --exec $DAEMON -- $DAEMON_OPTS
        return $errcode

stop_server() {
# Stop the process using the wrapper
        if [ -z "$DAEMONUSER" ] ; then
            start-stop-daemon --stop --quiet --pidfile $PIDFILE
            rm $PIDFILE
# if we are using a daemonuser then look for process that match
            start-stop-daemon --stop --quiet --pidfile $PIDFILE \
                        --user $DAEMONUSER \
                        --exec $DAEMON

        return $errcode

reload_server() {
    [ ! -f "$PIDFILE" ] && return 1
    pid=pidofproc $PIDFILE # This is the daemon's pid
    # Send a SIGHUP
    kill -1 $pid
    return $?

force_stop() {
# Force the process to die killing it manually
        [ ! -e "$PIDFILE" ] && return
        if running ; then
                kill -15 $pid
        # Is it really dead?
                sleep "$DIETIME"s
                if running ; then
                        kill -9 $pid
                        sleep "$DIETIME"s
                        if running ; then
                                echo "Cannot kill $NAME (pid=$pid)!"
                                exit 1
        rm -f $PIDFILE

case "$1" in
        log_daemon_msg "Starting $DESC $NAME"
        # Check if it's running first
        if running ;  then
            log_progress_msg "apparently already running"
            log_end_msg 0
            exit 0
        if start_server ; then
            # NOTE: Some servers might die some time after they start,
            # this code will detect this issue if STARTTIME is set
            # to a reasonable value
            [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
            if  running ;  then
                # It's ok, the server started and is running
                log_end_msg 0
                # It is not running after we did start
                log_end_msg 1
            # Either we could not start it
            log_end_msg 1
        log_daemon_msg "Stopping $DESC" "$NAME"
        if running ; then
            # Only stop the server if we see it running
            stop_server || errcode=$?
            log_end_msg $errcode
            # If it's not running don't do anything
            log_progress_msg "apparently not running"
            log_end_msg 0
            exit 0
        # First try to stop gracefully the program
        $0 stop
        if running; then
            # If it's still running try to kill it more forcefully
            log_daemon_msg "Stopping (force) $DESC" "$NAME"
            force_stop || errcode=$?
            log_end_msg $errcode
        log_daemon_msg "Restarting $DESC" "$NAME"
        stop_server || errcode=$?
        # Wait some sensible amount, some server need this
        [ -n "$DIETIME" ] && sleep $DIETIME
        start_server || errcode=$?
        [ -n "$STARTTIME" ] && sleep $STARTTIME
        running || errcode=$?
        log_end_msg $errcode

        log_daemon_msg "Checking status of $DESC" "$NAME"
        if running ;  then
            log_progress_msg "running"
            log_end_msg 0
            log_progress_msg "apparently not running"
            log_end_msg 1
            exit 1
  # Use this if the daemon cannot reload
        log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
        log_warning_msg "cannot re-read the config file (use restart)."
  # And this if it cann
          # If the daemon can reload its config files on the fly
          # for example by sending it SIGHUP, do it here.
          # If the daemon responds to changes in its config file
          # directly anyway, make this a do-nothing entry.
          # log_daemon_msg "Reloading $DESC configuration files" "$NAME"
          # if running ; then
          #    reload_server
          #    if ! running ;  then
          # Process died after we tried to reload
          #       log_progress_msg "died on reload"
          #       log_end_msg 1
          #       exit 1
          #    fi
          # else
          #    log_progress_msg "server is not running"
          #    log_end_msg 1
          #    exit 1
          # fi

        echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
        exit 1

exit 0

ubuntu:/MTConnect_Agent_Simulator_DIR/agent_startup_DIR# ./agent debug
MTConnect Agent Version - built on Mon Jul 30 09:41:08 2012
2012-07-30T15:57:01.307850Z: INFO  [0] init.config: Starting agent on port 5000
2012-07-30T15:57:01.317817Z: INFO  [0] init.config: Adding adapter for VMC-3Axis on localhost:7878
2012-07-30T15:57:01.318606Z: DEBUG [1] input.connector: Connecting to data source: localhost on port: 7878
2012-07-30T15:57:01.325697Z: DEBUG [1] input.connector: Sending initial PING
2012-07-30T15:57:01.362692Z: DEBUG [1] input.connector: Received PONG, starting heartbeats every 10000ms

http://localhost:5000/sample  in the browser  (note port 5000)

ubuntu:/MTConnect_Agent_Simulator_DIR/simulator# ls

run_scenario.rb  simple_scenario_1.txt     simulator.rb  VMC-3Axis-Log.txt  VMC-3Axis.xml

ubuntu:/MTConnect_Agent_Simulator_DIR/simulator# ruby -v run_scenario.rb -l VMC-3Axis-Log.txt
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
Waiting on 7878
Client connected


   * -v is verbose
   * run_scenario.rb   is exactly that a run scenario
   *  -l VMC-3Axis-Log.txt is to run that long file for simulator data

ubuntu:~dave# ln -s /home/dave/MTConnect_cppagent-version_1.2.0.14_DIR/mtconnect-cppagent-a100593 /MTConnect_Agent_Simulator_DIR

 netstat -anp | grep "LIST"  to see what ports are open

ubuntu:/MTConnect_Agent_Simulator_DIR/simulator# netstat -anp | grep "LIST"
tcp        0      0  *               LISTEN      1328/dnsmasq  
tcp        0      0    *               LISTEN      4121/inetd    
tcp        0      0 *               LISTEN      595/cupsd     
tcp        0      0  *               LISTEN      3622/ruby     
tcp        0      0  *               LISTEN      3627/agent    
tcp6       0      0 :::80                   :::*                    LISTEN      1024/apache2  
tcp6       0      0 ::1:631                 :::*                    LISTEN      595/cupsd  

 to make things easier, here are some simple startup scripts:

Simulator_DIR/simulator# more start_simulator
ruby -v run_scenario.rb -l VMC-3Axis-Log.txt

ubuntu:/MTConnect_Agent_Simulator_DIR/agent_startup_DIR# more start_agent_debug_mode

./agent debug

ubuntu:/MTConnect_Agent_Simulator_DIR/simulator# ./start_simulator
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
Waiting on 7878
Client connected

MTConnect Agent Version - built on Mon Jul 30 09:41:08 2012
2013-01-27T15:57:13.055418Z: INFO  [0] init.config: Starting agent on port 5000
2013-01-27T15:57:13.060496Z: INFO  [0] init.config: Adding adapter for VMC-3Axis on localhost:7878
2013-01-27T15:57:13.061009Z: DEBUG [1] input.connector: Connecting to data source: localhost on port: 7878
2013-01-27T15:57:13.069044Z: DEBUG [1] input.connector: Sending initial PING
2013-01-27T15:57:13.108968Z: DEBUG [1] input.connector: Received PONG, starting heartbeats every 10000ms

I love how Will sends out a ping and then gets a pong back :-)

No comments:

Post a Comment