chan-sccp / PAMI

Fork of PHP Asterisk Manager Interface ( AMI ) supports synchronous command ( action )/ responses and asynchronous events using the pattern observer-listener. Supports commands with responses with multiple events. Very suitable for development of operator consoles and / or asterisk / channels / peers monitoring through SOA, etc
http://marcelog.github.com/PAMI
Apache License 2.0
27 stars 21 forks source link

Parse key `Output` for CommandAction #6

Closed alexpts closed 5 years ago

alexpts commented 5 years ago

RawContent:

Response: Success
ActionID: 1573107661.8815
Message: Command output follows
Output: conversionq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:5, C:0, A:0, SL:0.0%, SL2:0.0% within 20s
Output:    No Members
Output:    No Callers
Output: 
Output: callsq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0%, SL2:0.0% within 0s
Output:    No Members
Output:    No Callers
Output: 
Output: marketplaceq has 0 calls (max unlimited) in 'linear' strategy (0s holdtime, 0s talktime), W:5, C:0, A:0, SL:0.0%, SL2:0.0% within 20s
Output:    No Members
Output:    No Callers
Output: 
Output: outsourceq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:5, C:0, A:0, SL:0.0%, SL2:0.0% within 20s
Output:    No Members
Output:    No Callers
Output: 
Output: partnerq has 0 calls (max unlimited) in 'ringall' strategy (6s holdtime, 43s talktime), W:5, C:4, A:1, SL:75.0%, SL2:80.0% within 20s
Output:    Members: 
Output:       SIP/208 (ringinuse disabled) (dynamic) (Unavailable) has taken no calls yet
Output:    No Callers
Output: 
Output: testq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0%, SL2:0.0% within 0s
Output:    No Members
Output:    No Callers
Output: 
Output: callcenter has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:5, C:0, A:0, SL:0.0%, SL2:0.0% within 20s
Output:    No Members
Output:    No Callers
Output: 
Output: trashq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0%, SL2:0.0% within 0s
Output:    No Members
Output:    No Callers
Output: 
Output: opinionq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0%, SL2:0.0% within 0s
Output:    No Members
Output:    No Callers
Output: 
Output: 

Response keys:

response => "Success"
actionId => "1573107661.8815"
message => "Command output follows"
output => null

I expected array with messages in output field.

dkgroot commented 5 years ago

Hi Alex,

CommandAction is just going to execute a CLI command, you give it and return the CLI output (string) to you as far as i know. The is no real way to format the returned string into anything sensible.

However CommandAction is handled a little differently then other messages, because it returns a CommandResponse. So if we should do some extra processing, we can do that here. You can use getCommandOutput() to retrieve the output in question, no need to go through the rawContent.

If you mean output is currently not being filled/processed correctly and getCommandOutput does not work then i mis understood, and we should correct/fix that. Let me know if i misunderstood and i can look into it later today.

dkgroot commented 5 years ago

I think i know what might be going wrong here, we are only setting $output to the last line of the returned output. For example running:

$ca = new CommandAction('sip show peers');
$cr = $a->send($ca);
var_dump($cr);
echo($cr->getCommandOutput());

Would return:

2 sip peers [Monitored: 1 online, 1 offline Unmonitored: 0 online, 0 offline]

Instead of:

Name/username             Host                                    Dyn Forcerport Comedia    ACL Port     Status      Description                      
1234/1234                 (Unspecified)                            D  Yes        Yes         A  0        UNKNOWN                                      
1235/1235                 192.168.178.100                          D  Yes        Yes         A  5060     "...
2 sip peers [Monitored: 1 online, 1 offline Unmonitored: 0 online, 0 offline]

That will have to be fixed.

alexpts commented 5 years ago

Method getCommandOutput returns empty string

alexpts commented 5 years ago

Message->setSanitizedKey rewrite all Output key for all line

alexpts commented 5 years ago

Each line from raw content parse via Message->setSanitizedKey.

Key output may many times in response. Response has only last output message instead array with all output message for this case

alexpts commented 5 years ago
object(PAMI\Message\Response\CommandResponse)[326]
  protected 'events' => 
    array (size=0)
      empty
  protected 'completed' => boolean true
  protected 'rawContent' => string 'Response: Success
ActionID: 1573115294.4539
Message: Command output follows
Output: conversionq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:5, C:0, A:0, SL:0.0%, SL2:0.0% within 20s
Output:    No Members
Output:    No Callers
Output: 
Output: callsq has 0 calls (max unlimited) in 'ringall' strategy (0s holdtime, 0s talktime), W:0, C:0, A:0, SL:0.0%, SL2:0.0% within 0s
Output:    No Members
Output:    No Callers
Output: 
Output: marketplaceq has 0 calls (max unl'... (length=1943)
  protected 'channelVariables' => 
    array (size=1)
      'default' => 
        array (size=0)
          empty
  protected 'statusVariables' => 
    array (size=1)
      'default' => 
        array (size=0)
          empty
  protected 'lines' => 
    array (size=0)
      empty
  protected 'variables' => 
    array (size=0)
      empty
  protected 'keys' => 
    array (size=4)
      'response' => string 'Success' (length=7)
      'actionid' => string '1573115294.4539' (length=15)
      'message' => string 'Command output follows' (length=22)
      'output' => null
  protected 'createdDate' => int 1573115294
  public 'eventsCount' => int 0

I think key output must be array of strings

dkgroot commented 5 years ago

You are right, we should be able to include that in CommandResponse.php. Have not had time though.

dkgroot commented 5 years ago

I have a simple solution, using your pointers, which seems to work, testing now.

dkgroot commented 5 years ago

Update Message/Response/CommandResponse.php:

Git Checkout this temporary branch

git pull
git fetch -a
git branch
git checkout issues/6

move back to master branch afterward testing:

git checkout master

If it works and is useable, we can update the docs and merge it back into the master branch. Please give it a go. Thanks for helping out guiding me in the right direction, has been a while since i worked on the code :-) if you have some ideas to do it in a nicer way, let me know

alexpts commented 5 years ago

Thanks! I will be wait new release for composer :)

dkgroot commented 5 years ago

Ooh it would not generate one automatically, because i put it in a seperate branch... I will merge it right now (should be fine :-))

BTW: You can use composer to point at the master branch using: composer.json

    ....
    "repositories": [
        {
            "url": "https://github.com/chan-sccp/PAMI.git",
            "type": "git"
        }
    ],
    "require-dev": {
        "chan-sccp/pami": "dev-master",
    }
dkgroot commented 5 years ago

Pushed and merged the issues/6 branch into master ! Please test/check and re-open if necessary Thanks for the issue report and helping in fixing it.

dkgroot commented 5 years ago

@alexpts We are trimming the result and 'loosing' the indentation in your 'queue show' example. I am not sure how important that is you. If need be you can still get that from the rawContent of course.

dkgroot commented 5 years ago

@alexpts I have updated composer/packagist package so you can update to v2.0.4 when you want to.